If you are looking to find or replace items in a string, Python has several built-in-methods that can help you search a target string for a specified substring.

.find() Method

Note: start and end are optional arguments.

From the above syntax, we can observe that the .find() method takes the desired substring as the mandatory argument. We can specify the other two arguments: an inclusive starting position & an exclusive ending position.

Substring Search

In the example code, you search for Waldo in the string Where's Waldo?. The .find() method returns the lowest index in the string where it can find the substring, in this case, eight.

If you search for Wenda, it returns -1 since the substring is not found.

Now let’s see if we can find Waldo between characters zero and five. In the code, we specify the starting position zero & ending position as six, since this position is not inclusive.

The .find() method does not find the substring and returns -1, as shown above.

.index() Method

Note: start and end are optional arguments.

From the above syntax, we can observe that the .index() method takes the desired substring as a mandatory argument. It can take optional starting & ending positions as well.

Substring Search

In the example, we search again for Waldo using .index().

We get eight again. When we look for a substring that is not there, we have a difference.

The .index() method raises an exception, as we can see in the output. We can handle this using the try except block.

Above, you can observe the syntax. The try part will test the given code. If any error appears, the except part will be executed, obtaining the following output as a result.

.count() Method

The .count() method searches for a specified substring in the target string. It returns the number of non-overlapping occurrences. In simple words, how many times the substring is present in the string.

Syntax

The syntax of .count() is very similar to the other methods, as we can observe.

Note: start and end are optional arguments.

Substring Count

In the example, we use the .count() method to get how many times fruit appears.

In the output, we see that it is two.

We can limit the occurrences of fruit between character zero & fifteen of the string, as we can observe in the code below.

The method will return 1. Remember that the starting position is inclusive, but the ending is not.

.replace Method

Sometimes you will want to replace occurrences of a substring with a new substring. In this case, Python provides us with the .replace method.

Syntax

Note: count is an optional argument.

As we see in the syntax above, it takes three arguments: the substring being replaced, the new string to replace it, & an optional number that indicates how many occurrences to replace.

Replacing a Substring

In the example code, we replace the substring house with car.

The method will return a copy with all house substrings replaced.

Replacing a Specific Number of Occurrences

In this example, we specified that we only want two of the occurrences to be replaced.

In the output, we see that the method returns a copy of the string with the first two occurrences of house replaced by car.

RELATED LINKS

Data Scientist & Machine Learning Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store