Skip to content

7.3 String methods

Finding and Replacing

  • Recall that string objects are immutable -- once created, strings can not be changed.
  • To update a string variable, a new string object must be created and bound to the variable name, replacing the old object.
  • The replace string method provides a simple way to create a new string by replacing all occurrences of a substring with a new substring.

    • replace(old, new) Returns a copy of the string with all occurrences of the substring old replaced by the string new. The old and new arguments may be string variables or string literals.
    • replace(old, new, count) Same as above, except only replaces the first count occurrences of old.

    7%203%20String%20methods%20c44b63d484c1424390ad537959e2d9b1/Untitled.png

    Some methods are useful for finding the position of where a character or substring is located in a string:

    • find(x) Returns the index of the first occurrence of item x in the string, else returns -1. x may be a string variable or string literal. Recall that in a string, the index of the first character is 0, not 1. If my_str is 'Boo Hoo!':
      • my_str.find('!') # Returns 7
      • my_str.find('Boo') # Returns 0
      • my_str.find('oo') # Returns 1 (first occurrence only)
    • find(x, start) Same as find(x), but begins the search at index start:
      • my_str.find('oo', 2) # Returns 5
    • find(x, start, end) Same as find(x, start), but stops the search at index end - 1:
      • my_str.find('oo', 2, 4) # Returns -1 (not found)
    • rfind(x) Same as find(x) but searches the string in reverse, returning the last occurrence in the string.

    Another useful function is count, which counts the number of times a substring occurs in the string:

    • count(x) -- Returns the number of times x occurs in the string.
      • my_str.count('oo') # Returns 2
    • Note that methods such as find() and rfind() are useful only for cases where a programmer needs to know the exact location of the character or substring in the string. If the exact position is not important, then the in membership operator should be used to check if a character or substring is contained in the string:

7%203%20String%20methods%20c44b63d484c1424390ad537959e2d9b1/Untitled%201.png

Comparing Strings

  • String objects may be compared using relational operators (<, <=, >, >=), equality operators (==, !=), membership operators (in, not in), and identity operators (is, is not).

7%203%20String%20methods%20c44b63d484c1424390ad537959e2d9b1/Untitled%202.png

  • The identity operators (is, is not) determine whether the two arguments are bound to the same object
  • A common error is to use an identity operator in place of an equality operator.
    • Ex: A programmer may write name is 'Amy Adams', intending to check if the value of name is the same as the literal 'Amy Adams'.
    • Instead, the Python interpreter creates a new string object from the string literal on the right, and compares the identity of the new object to the name object, which returns False.
  • Good practice is to always use the equality operator== when comparing values.

7%203%20String%20methods%20c44b63d484c1424390ad537959e2d9b1/Untitled%203.png

Methods to check a string value that returns a True or False Boolean value:

  • isalnum() -- Returns True if all characters in the string are lowercase or uppercase letters, or the numbers 0-9.
  • isdigit() -- Returns True if all characters are the numbers 0-9.
  • islower() -- Returns True if all cased characters are lowercase letters.
  • isupper() -- Return True if all cased characters are uppercase letters.
  • isspace() -- Return True if all characters are whitespace.
  • startswith(x) -- Return True if the string starts with x.
  • endswith(x) -- Return True if the string ends with x.

**Note that the methods islower() and isupper() ignore non-cased characters. Ex: 'abc?'.islower() returns True, ignoring the question mark.

Creating new strings from a string

Methods to create new strings:

  • capitalize() -- Returns a copy of the string with the first character capitalized and the rest lowercased.
  • lower() -- Returns a copy of the string with all characters lowercased.
  • upper() -- Returns a copy of the string with all characters uppercased.
  • strip() -- Returns a copy of the string with leading and trailing whitespace removed.
  • title() -- Returns a copy of the string as a title, with first letters of words capitalized.
  • Good practice when reading user-entered strings is to apply transformations when reading in data (such as input), as opposed to later in the program.
    • Applying transformations immediately limits the likelihood of introducing bugs because the user entered an unexpected string value.