Processing math: 100%
Skip to main content
Library homepage
 

Text Color

Text Size

 

Margin Size

 

Font Type

Enable Dyslexic Font
Engineering LibreTexts

8.1: String Operations

( \newcommand{\kernel}{\mathrm{null}\,}\)

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
  • 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
  • ABBA
  • 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".


    This page titled 8.1: String Operations is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by OpenStax via source content that was edited to the style and standards of the LibreTexts platform.

    Support Center

    How can we help?