8.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.
8.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.
Method |
Syntax |
Description |
---|---|---|
|
Returns the number of times |
|
|
Returns the index of the first occurrence of |
|
|
Returns the index of the first occurrence of |
|
|
Returns a copy of the given string, with all uppercase letters converted to lowercase. |
|
|
Returns a copy of |
|
|
Splits the string at each occurrence of |
|
|
Returns a copy of the given string with leading and trailing
|
|
|
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'
:
word.upper()
returnsPYTHON
.word.replace('n', 'n!!!')
returnsPython!!!
Chaining the methods together as word.replace('n', 'n!!!').upper()
returns PYTHON!!!
.
What would word.lower().strip('p').find('t')
return?
8.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:
In line 2,
nonprofit.lower()
evaluates to"launchcode"
and assigns that string to the variablelowercase
.In line 3,
nonprofit.replace('a', '')
removes the letter'a'
and assigns a new string tomeal_plan
.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.
8.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)
|
Pythqn rocks
ythqn rqcks
ythqn rqcks!
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)
|
" The LaunchCode Foundation "
"The LaunchCode Foundation"
"TheLaunchCodeFoundation"
" The LaunchCode Foundation"
Question
Given word = "Rutabaga"
, what is the value returned by
word.lower().strip('r').find('t')
?
'utabaga'
2
1
't'