Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

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

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 permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a 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 digital format, then you must 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

© Jul 30, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo 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.