Skip to ContentGo to accessibility page

8.1 String operations

Learning objectives

By the end of this section you should be able to

  • Compare strings using logical and membership operators.
  • Use lower() and upper() string methods to convert string values to lowercase and uppercase characters.

String comparison

String values can be compared using logical operators (<, <=, >, >=, ==, !=) and membership operators (in and not in). When comparing two string values, the matching characters in two string values are compared sequentially until a decision is reached. For comparing two characters, ASCII values are used to apply logical operators.

Operator Description Example Output Explanation

> or >=

Checks whether the first string value is greater than (or greater than or equal to) the second string value.

"c" > "d"

False

When comparing "c" operand to "d" operand, the ASCII value for "c" is smaller than the ASCII value for "d". Therefore, "c" < "d". The expression "c" > "d" evaluates to False.

< or <=

Checks whether the first string value is less than (or less than or equal to) the second string value.

"ab" < "ac"

True

When comparing "ab" operand to "ac" operand, the first characters are the same, but the second character of "ab" is less than the second character in "ac" and as such "ab" < "ac".

==

Checks whether two string values are equal.

"aa" == "aa"

True

Since all characters in the first operand and the second operand are the same, the two string values are equal.

!=

Checks whether two string values are not equal.

"a" != "b"

True

The two operands contain different string values ("a" vs. "b"), and the result of checking whether the two are not the same evaluates to True.

in

Checks whether the second operand contains the first operand.

"a" in "bc"

False

Since string "bc" does not contain string "a", the output of "a" in "bc" evaluates to False.

not in

Checks whether the second operand does not contain the first operand.

"a" not in "bc"

True

Since string "bc" does not contain string "a", the output of "a" not in "bc" evaluates to True.

Table 8.1 Comparing string values.

Concepts in Practice

Using logical and membership operators to compare string values

1.
What is the output of ("aaa" < "aab")?
  1. True
  2. False
2.
What is the output of ("aa" < "a")?
  1. True
  2. False
3.
What is the output of ("aples" in "apples")?
  1. undefined
  2. True
  3. False

lower() and upper()

Python has many useful methods for modifying strings, two of which are lower() and upper() methods. The lower() method returns the converted alphabetical characters to lowercase, and the upper() method returns the converted alphabetical characters to uppercase. Both the lower() and upper() methods do not modify the string.

Example 8.1

Converting characters in a string

In the example below, the lower() and upper() string methods are called on the string variable x to convert all characters to lowercase and uppercase, respectively.

    x = "Apples"

    # The lower() method converts a string to all lowercase characters
    print(x.lower())

    # The upper() method converts a string to all uppercase characters
    print(x.upper())

The above code's output is:

    apples
    APPLES

Concepts in Practice

Using lower() and upper()

4.
What is the output of "aBbA".lower()?
  1. abba
  2. ABBA
  3. abbA
5.
What is the output of "aBbA".upper()?
  1. abba
  2. ABBA
  3. ABbA
  4. aBBA
6.
What is the output of ("a".upper() == "A")?
  1. True
  2. False

Try It

Number of characters in the string

A string variable, s_input, is defined. Use lower() and upper() to convert the string to lowercase and uppercase, and print the results in the output. Also, print the number of characters in the string, including space characters.

Try It

What is my character?

Given the string, s_input, which is a one-character string object, if the character is between "a" and "t" or "A" and "T", print True. Otherwise, print False.
Hint: You can convert s_input to lowercase and check if s_input is between "a" and "t".

Citation/Attribution
Reuse and redistribution of this content in digital or print format:
  • This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's prior written permission.
  • This book uses the Creative Commons Attribution-NonCommercial-ShareAlike License, which means that you can reuse and modify the material only for noncommercial purposes, must attribute OpenStax, and must distribute any derivative works under the same license.
  • Any commercial printing of this textbook, including using a local or custom printer, must be approved by OpenStax, and proper citation provided.
  • OpenStax-copyrighted images, activities, assessments, and similar components of this book are subject to the same licensing – CC-BY-NC-SA. They can be used for noncommercial purposes with attribution. Commercial use requires permission.
  • Permission requests: Anyone who intends to incorporate this content (including text, images, and other components) into large language models, use it in AI offerings, use it commercially (including in print), and/or has questions about another use case is welcome to complete our reuse request form.
Attribution information
  • If you are redistributing all or part of this book in a noncommercial print format, then you must include on every physical page the following attribution:

    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction

  • If you are redistributing all or part of this book in a noncommercial digital format, then for every page that includes OpenStax content, you must license the derivative work under the same CC-BY-NC-SA license as the original, and include on every digital page view the following attribution:

    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction

Citation information

The information below includes the information needed to generate citations in most major styles (APA, MLA, etc.); you must reformat and organize the information as needed to fit the requirements of the style. Use the information below to generate a citation. We recommend using a citation tool such as this one.

© Apr 23, 2026 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike License. The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo, and Rice University name, and Rice University logo trademarks, or wordmarks are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.