Skip to main content
Engineering LibreTexts

8.1: String Operations

  • Page ID
    117571
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\dsum}{\displaystyle\sum\limits} \)

    \( \newcommand{\dint}{\displaystyle\int\limits} \)

    \( \newcommand{\dlim}{\displaystyle\lim\limits} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

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

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

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

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \(\newcommand{\longvect}{\overrightarrow}\)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)
    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.

    Table 8.1 Comparing string values.
    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.

    Concepts in Practice \(\PageIndex{1}\): Using logical and membership operators to compare string values

    What is the output of ("aaa" < "aab")?

    1. True
    2. False
    Answer

    a. Since a's ASCII value is less than b's ASCII value, the output for "aaa" < "aab" is True.

    Concepts in Practice \(\PageIndex{2}\): Using logical and membership operators to compare string values

    What is the output of ("aa" < "a")?

    1. True
    2. False
    Answer

    b. Since "a" is shorter than "aa" , and the prefix "aa" with length 1 equals the second operand ( "a" ), the shorter string is considered smaller. As such, "aa" < "a" evaluates to False.

    Concepts in Practice \(\PageIndex{3}\): Using logical and membership operators to compare string values

    What is the output of ("aples" in "apples")?

    1. undefined
    2. True
    3. False
    Answer

    c. While all characters in "aples" exist in "apples" , the characters are not sequential, so the string "aples" is not a substring of "apples" . Therefore, "aples" in "apples" returns 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 \(\PageIndex{4}\)

    What is the output of "aBbA".lower()?

    1. abba
    2. ABBA
    3. abbA
    Answer

    a. "aBbA".lower() converts all characters to lowercase, and the output is "abba" .

    Concepts in Practice \(\PageIndex{5}\)

    What is the output of "aBbA".upper()?

    1. abba
    2. ABBA
    3. ABbA
    4. aBBA
    Answer

    b. "aBbA".upper() converts all characters to uppercase, and the output is "ABBA" .

    Concepts in Practice \(\PageIndex{6}\)

    What is the output of ("a".upper() == "A")?

    1. True
    2. False
    Answer

    a. The upper() converts "a" to uppercase ( "A" ). When checking "a".upper() == "A", the output is True.

    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.

    Interactive Code
    s_input =  "Today is a great DAY!"
    
    
    
     
    Answer

    s_input = "Today is a great DAY!"

    print(s_input.lower())
    print(s_input.upper())

    print(len(s_input))

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

    Interactive Code
    s_input = "B"
     
    Answer

    s_input = "B"
    if s_input.lower() >= 'a' and s_input.lower() <= 't':
    print("True")
    else:
    print("False")


    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.