Python Beginner Concepts Tutorial

String Methods

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

In [9]:
name = "dan"

We call the upper method on our name to capitalize all characters of the string.

In [10]:

The empty parentheses of this method, and others used below, indicate that this method takes no arguments.

Capitalize first value in a string

In [11]:
name = "dan"

We call the capitalize method on our name to capitalize just the first value - the letter d.

In [12]:

Lowercase all values in a string

In [13]:
shout = "HEY"

We call the lower method on shout to make all characters of the string lowercase.

In [14]:

Find index of value(s) in a string

In [15]:
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.

In [16]:

Let's say we have a string with multiple a's in it.

In [17]:
fruit = "banana"

We still return the first index of the letter a in the string fruit.

In [18]:

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.

In [19]:
fruit.find("a", 2)

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.

In [20]:

Split string into list elements

Often times, we want to evaluate words in a sentence - one by one.

In [21]:
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.

In [22]:
['Hey', 'how', 'are', 'you']

We can also split by a different character.

In [23]:
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.

In [24]:
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.

In [25]:
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".

In [29]:
text.replace("Uh", "")
" I'm not sure of the answer. Have you thought about 6?  or 7?"