One of the most common operations that programmers use on strings is to check whether a string contains some other string.
If you are coming to Python from Java, for instance, you might have used the contains method to check if some substring exists in another string.
In Python, there are two ways to achieve this.
First: Using the in operator
The easiest way is via Python’s in operator.
Let’s take a look at this example.
>>> str = "Messi is the best soccer player" >>> "soccer" in str True >>> "football" in str False
As you can see, the in operator returns True when the substring exists in the string.
Otherwise, it returns false.
This method is very straightforward, clean, readable, and idiomatic.
Second: Using the find method
Another method you can use is the string’s find method.
Unlike the in operator which is evaluated to a boolean value, the find method returns an integer.
This integer is essentially the index of the beginning of the substring if the substring exists, otherwise -1 is returned.
Let’s see the find method in action.
>>> str = "Messi is the best soccer player" >>> str.find("soccer") 18 >>> str.find("Ronaldo") -1 >>> str.find("Messi") 0
One cool thing about this method is you can optionally specify a start index and an end index to limit your search within.
>>> str = "Messi is the best soccer player" >>> str.find("soccer", 5, 25) 18 >>> str.find("Messi", 5, 25) -1
Notice how a -1 was returned for “Messi” because you are limiting your search to the string between indices 5 and 25 only.
Python 3 Cheat Sheet for Beginners
Download a comprehensive cheat sheet for beginners with extensive code examples that covers all the topics that you need to learn.
Some Advanced Stuff
Assume for a second that Python has no built-in functions or methods that would check if a string contains another string.
How would you write a function to do that?
Well, an easy way is to brute force by checking if the substring exists starting from every possible position in the original string.
For larger strings, this process can be really slow.
There are better algorithms for string searching.
I highly recommend this article from TopCoder if you want to learn more and dive deeper into string searching algorithms.
For more coverage of other string searching algorithms not covered in the previous article, this wikipedia page is great.
If you go through the previous articles and study them, your next question would be “well what algorithm does Python actually use?”
These kinds of questions almost always require digging into the source code.
But you are in luck because Python’s implementation is open source.
Alright, let’s dig into the code.
Perfect, I am happy the developers commented their code 🙂
It is very clear now that the find method uses a mix of boyer-moore and horspool algorithms.
You can use the in operator or the string’s find method to check if a string contains another string.
The in operator returns True if the substring exists in the string. Otherwise, it returns False.
The find method returns the index of the beginning of the substring if found, otherwise -1 is returned.
Python’s implementation (CPython) uses a mix of boyer-moore and horspool for string searching.
Check out the Courses section!
- The Python Learning Path (From Beginner to Mastery)
- Learn Computer Science (From Zero to Hero)
- Coding Interview Preparation Guide
- The Programmer’s Guide to Stock Market Investing
- How to Start Your Programming Blog?
Are you Beginning your Programming Career?
I provide my best content for beginners in the newsletter.
- Python tips for beginners, intermediate, and advanced levels.
- CS Career tips and advice.
- Special discounts on my premium courses when they launch.
And so much more…
Pretty explanatory and it helped me with some question I had!
Found a mistake in this article though. Where you have “Messi is the best soccer player” by accident you wrote Messi instead of Ronaldo. Common mistake 😉
haha I think you need to reconsider this 🙂
I agree with João, very good article. But you should correct the mistake cited by him. kkkk
Thanks Guilherme. haha So all Cristiano’s fans are ganging up against me now? 🙂 where are Messi’s fans?
here i am
Messi gang rise up!!
ahhaha Messi == puppy, Ronaldo == Dinasour
Good article, helps a lot!
Thanks a lot! 🙂
Thanks for the article. there is something wrong.
Messi is not the best player 😀
The code you wrote won’t run… But I replaced “Messi” with “Ronaldo” and it worked fine!
The code is not running because you have an ancient python interpreter 😉