Get startedGet started for free

Finding and replacing

1. Finding and replacing

Finally, we'll talk about different methods that allow us to search or replace substrings.

2. Finding substrings

Python has several built-in methods that will help you search a target string for a specified substring. The first method is dot find. As you can see in the slide, it takes the desired substring as a mandatory argument. You can specify two other arguments. An inclusive starting position and an exclusive ending position. In the example code, we search for Waldo in the string Where's Waldo?. The dot find method returns the lowest index in the string where it can find the substring, in this case, eight. If we search for Wenda, the substring is not found and the method returns minus one.

3. Finding substrings

Now, we check if we can find Waldo between characters number zero and five. In the code, we specify the starting position, zero, and the ending position, six, because this position is not inclusive. The dot find method will not find the substring and returns minus one as we see in the output.

4. Index function

The dot index method is identical to dot find. In the slide, we see that it takes the desired substring as mandatory argument. It can take optional starting and ending positions as well. In the example, we search again for Waldo using dot index. We get eight again. When we look for a substring that is not there, we have a difference. The dot index method raises an exception, as we can see in the output.

5. Index function

We can handle this using the try except block. In the slide, 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.

6. Counting occurrences

The dot 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. The syntax of dot count is very similar to the other methods as we can observe. In the example, we use the dot count method to get how many times fruit appears. In the output, we see that is two. Then, we limit the occurrences of fruit between character zero and fifteen of the string as we can observe in the code. The method will return 1. Remember that starting position is inclusive, but the ending is not.

7. Replacing substrings

Sometimes you will want to replace occurrences of a substring with a new substring. In this case, Python provides us with the dot replace method. As we see in the slide, it takes three arguments: the substring to replace, the new string to replace it, and an optional number that indicates how many occurrences to replace. In the example code, we replace the substring house with car. The method will return a copy with all house substrings replaced. In the next example, we specified that we only want 2 of the occurrences to be replaced. We see in the output that the method return a copy of the string with the first two occurrences of house replaced by car.

8. Wrapping up

In this chapter, we walked through learning how to manipulate strings, a valuable skill for any data science project. You saw how to slide, concatenate and adjust cases of strings, how to split them into pieces and join them back together, how to remove characters and finally how to find, count and replace occurrences of substrings.

9. Let's practice!

Now, it's your time to put the concepts into practice.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.