7.5. String Methods

The str data type includes a special group of operations, called methods, that we can use to make routine tasks easier. A method performs a specific action on the data within an object. For strings, the methods usually create a new string from the characters of the original.

Python provides many useful methods for string objects.

7.5.1. Common String Methods

Here we present the most commonly-used string methods. Clicking on the name of any method leads to examples and a more detailed description.

Common String Methods
Method Syntax Description
count string_name.count(search_string) Returns the number of times search_string occurs in string_name.
find string_name.find(a_string) Returns the index of the first occurrence of a_string in string_name. The method returns -1 if a_string is not found.
index string_name.index(a_string) Returns the index of the first occurrence of a_string in string_name. If a_string is not found, the method throws an error.
lower string_name.lower() Returns a copy of the given string, with all uppercase letters converted to lowercase.
replace string_name.replace(a_string, replacement) Returns a copy of string_name with every occurrence of a_string replaced by the replacement string.
split and list string_name.split('character') Splits the string at each occurrence of character, and returns a list of smaller strings.
strip string_name.strip('characters') Returns a copy of the given string with leading and trailing characters strings removed. By default, characters is a space.
upper string_name.upper() Returns a copy of the given string, with all lowercase letters converted to uppercase.

You can find complete lists of the Python string methods on W3Schools and the official Python documentation site.

Tip

String methods can be combined in a process called method chaining. Given word = 'Python':

  1. word.upper() returns PYTHON.
  2. word.replace('n', 'n!!!') returns Python!!!

Chaining the methods together as word.replace('n', 'n!!!').upper() returns PYTHON!!!.

What would word.lower().strip('p').find('t') return?

7.5.2. Strings Are Immutable

As we learned, strings are immutable. Therefore, string methods will NOT change the value of a string itself. Instead, they return a new string that is the result of the operation.

Let’s take a look at this behavior with the lower() and replace() methods.

Example

1
2
3
4
5
6
7
nonprofit = "LaunchCode"
lowercase = nonprofit.lower()
meal_plan = nonprofit.replace('a', '')

print(lowercase)
print(meal_plan)
print(nonprofit)

Console Output

launchcode
LunchCode
LaunchCode

Note the following:

  1. In line 2, nonprofit.lower() evaluates to "launchcode" and assigns that string to the variable lowercase.
  2. In line 3, nonprofit.replace('a', '') removes the letter 'a' and assigns a new string to meal_plan.
  3. Despite the actions in lines 2 and 3, the value of nonprofit stays the same.

This will be true for EVERY string method. Each method creates a brand new string, which we can print to the screen or assign to a variable. The original string never changes.

7.5.3. Check Your Understanding

Follow the links in the table above for the replace and strip methods. Review the content and then answer the following questions.

Question

What is printed by the following code?

1
2
3
4
text = "Python rocks!"
text.replace('o', 'q')
text.strip('!P')
print(text)
  1. Pythqn rocks
  2. ythqn rqcks
  3. ythqn rqcks!
  4. Python rocks!

Question

What is the value of the string printed by the following program?

1
2
3
4
org = "  The LaunchCode Foundation "
trimmed = org.strip()

print(trimmed)
  1. " The LaunchCode Foundation "
  2. "The LaunchCode Foundation"
  3. "TheLaunchCodeFoundation"
  4. " The LaunchCode Foundation"

Question

Given word = "Rutabaga", what is the value returned by word.lower().strip('r').find('t')?

  1. 'utabaga'
  2. 2
  3. 1
  4. 't'