String Methods¶
Date published: 2018-03-25
Category: Python
Subcategory: Beginner Concepts
Tags: strings
Intro to String Methods¶
Methods are similar to a function in that they take arguments and return a value - but the syntax is different.
There are specific methods you can call on the string object.
The syntax is typically string_name.method()
.
Note, string methods modify the string and return a new string. This happens because strings are immutable meaning they can't be changed in-place.
Capitalize all values in a string¶
name = "dan"
We call the upper
method on our name
to capitalize all characters of the string.
name.upper()
'DAN'
The empty parentheses of this method, and others used below, indicate that this method takes no arguments.
Capitalize first value in a string¶
name = "dan"
We call the capitalize
method on our name
to capitalize just the first value - the letter d.
name.capitalize()
'Dan'
Lowercase all values in a string¶
shout = "HEY"
We call the lower
method on shout
to make all characters of the string lowercase.
shout.lower()
'hey'
Find index of value(s) in a string¶
name = "dan"
We call the find
method on name
and pass in an argument of "a"
to find the first index of the letter a
.
name.find("a")
1
Let's say we have a string with multiple a's in it.
fruit = "banana"
We still return the first index of the letter a
in the string fruit
.
fruit.find("a")
1
By default, find
starts at the beginning of a string. But, the method can take a second argument on the index to start searching for a character.
We can start a search at index 2
, our letter n
, and find the index for the a
after that.
fruit.find("a", 2)
3
At index 3
, we found the second a
in our string.
We can also find the starting index of a substring - multiple characters in a string.
The starting index of "nana
should be at index 2
.
fruit.find("nana")
2
Split string into list elements¶
Often times, we want to evaluate words in a sentence - one by one.
greeting = "Hey how are you"
We can split our greeting
string into individual strings by each word. We call the split
method.
The default argument of split
is sep=None
. This splits our string by whitespace. Each whitespace element is removed from the result too.
greeting.split()
['Hey', 'how', 'are', 'you']
We can also split by a different character.
careful_greeting = "Hey, how are you"
We can separate by a comma and whitespace next to one another. The one instance of the whitespace with a following comma is removed from the result.
careful_greeting.split(sep=', ')
['Hey', 'how are you']
Replace value in string¶
Let's say we wanted to look more closely at someone's speech that was converted to text. There would likely be many "uh" values.
text = "Uh I'm not sure of the answer. Have you thought about 6? Uh or 7?"
The replace
method can take an original string and return an updated string with changed text.
We can call the replace
method on our original text
. The value of the first argument is the substring to be replaced. The value of the second argument is the replacement string.
We want to replace every instance of "Uh" with an empty string. Essentially, this just removes all mentions of the word "Uh".
text.replace("Uh", "")
" I'm not sure of the answer. Have you thought about 6? or 7?"