Strings I

Introduction to String Methods

In this lesson, you will learn methods to locate and find specific sub_strings within a string and manipulate strings.


You already know the join method to concatenate strings:

a = 'a' b = 'b' print('+'.join([a,b]))
Python
Output

But, there are many more useful methods for Python strings.

Sometimes, you need to know how many characters a string has. Use the len function to find out:

text = 'New York' print(len(text))
Python
Output

What will be the output?

text = 'Berlin' print(len(text))
Python

Let's combine the use of the len function with string slicing:


What will be the output?

text = 'Berlin' nChars = len(text) print(text[nChars-1])
Python

Another helpful string method is str.find(). It is used to locate the position of a single character or a sequence of characters within a string.

Here, we use the str.find() method to locate the position of the word 'the':

text = 'Without comments, the code becomes unreadable.' print(text.find('the'))
Python
Output

We get 18, which is the index number of the first character of the first (and only) occurrence of the word 'the' in our sentence.

This is useful, for example, if we don't want to count all the characters up to that word.

Now, we can use the result to extract the last part of the sentence using string slicing:

text = 'Without comments, the code becomes unreadable.' index = text.find('the') print(text[index:])
Python
Output

What will be the output?

text = 'Hello World' print(text.find('o'))
Python

What will be the output?

text = 'Hello World' index = text.find('r') print(text[:index])
Python

Additionally, we have the str.replace() method, which is used to replace certain characters or a sequence of characters within a string.

Here, we use the str.replace() method to replace the word 'pizza' with 'burgers':

text = 'I like apples, but I love pizza more!' print(text.replace('pizza','burgers'))
Python
Output

If there's more than one occurrence of the specified string, str.replace() will change all of them.

Here, we replace all occurrences of 'I' with 'you':

text = 'I like apples, but I love pizza more!' # note that this method is case sensitive # only capital I is replaced print(text.replace('I','You'))
Python
Output

It's important to note that this method, like all string methods, creates a new adjusted copy of the original string, while the original string remains unchanged.

Here, you can see that the original string remains the same even after we apply str.replace():

text = 'I like apples, but I love pizza more!' # print new adjusted copy of string print(text.replace('I','You')) # original string remains unchanged print(text)
Python
Output

Using str.replace(), you can also completely remove specific characters or sequences from a string:

text = 'I like apples, but I love pizza more!' # remove ' more' print(text.replace(' more','')) # remove all 'p' print(text.replace('p',''))
Python
Output

What will be the output?

text = 'Hello World' print(text.replace('l',''))
Python

What will be the output?

text = 'Hello World' text.replace('l','') print(text)
Python