7 Ways To Check If a Python String Contains Another String

Python string contains : If you are working on textual data, it may be interesting to check if a string contains another string.
In this tutorial, I will show you 6 ways to check if a string contains a substring. Each of these methods has its own advantages and disadvantages that I will detail in the parts of this tutorial below.
The IN
operator
The first way to check if a string contains another string is to use the in python operator. The in operator takes two arguments and returns the value True if the first argument is contained in the second argument. If not then the operator returns the value False.
Syntax
Here is the syntax for using the in operator :
string1 in string2
Example:
text="I like pokemon. My favorite is pikachu!"
string1="pikachu"
string2="ivysaur"
print("is the word pikachu found in the text?")
print(string1 in text)
print("is the word ivysaur found in the text?")
print(string2 in text)
Output: is the word pikachu found in the text? True is the word ivysaur found in the text? False
As you can see, the program returned True (Pikachu was found in the text). On the other hand, the word Ivysaur was not present in the text, so the program returned False.
Note : The in operator is a shortcut to call the __contains__() method of an object, which is called implicitly when using this operator (I will also explain how to use this function in the rest of the tutorial).
The String.find
method
The find method also checks that the string contains a sub-string.
The method can take up to 3 input parameters:
- str − Which corresponds to the searched string
- beg − Starting index, by default its 0.
- end − Ending index, by default its equal to the length of the string.
If the string is found then the method returns the value of the index otherwise it returns the value -1.
Syntax
Here is the syntax of the find method :
string1.find(string2)
Example:
str = "I like pokemon. My favorite is pikachu!"
print(str.find("pikachu"))
print(str.find("ivysaur"))
Output: 31 -1
You can also specify from which index you want to search for the substring, like this :
str = "I like pokemon. My favorite is pikachu!"
print(str.find("pikachu",32))
print(str.find("ivysaur",32))
Output: -1 -1
This time, the program returned -1 for the Pikachu substring, which is normal since the starting index is 32.
The String.count
method
The count method returns the number of retrieved occurrences of a substring in the string. This method also takes 3 parameters :
- sub − This corresponds to the searched string
- start − Optionnal – starts index. By default search starts from 0 index.
- end − Optionnal – ends index. By default search ends at the last index.
Syntax
Here is the syntax of the count method :
string1.count(string2)
Example:
str = "I like pokemon. My favorite is pikachu!"
print(str.count("i"))
print(str.count("i",10))
print(str.count("i",0,20))
Output: 4 3 1
Note: These functions are case sensitive. This means that upper and lower case letters are treated as separate elements. Be very careful.
The String.index
method
The index method can be used to find the starting index of the first occurrence of a substring in a chain. This can be useful if we need to know the position of the first character of the sub-string. This method takes 3 parameters :
- str − This specifies the string to be searched.
- beg − Starting index, by default 0.
- end − Ending index, by default its equal to the length of the string.
The method returns the index of the string if it is found, otherwise it raises a ValueError Exception.
Syntax
The syntax is as follows:
string1.index(string2)
Example:
string1 = "i like pokemon. My favorite is pikachu!"
print(string1.index("pikachu"))
print(string1.index("pikachu", 10))
print(string1.index("pikachu", 40))
Output: 31 31 Traceback (most recent call last): File "C:/Users/prog.py", line 5, in print(string1.index("pikachu", 40)) ValueError: substring not found
To raise the exception, you can use a try..except like this:
string1 = "i like pokemon. My favorite is pikachu!"
try:
print(string1.index("pikachu"))
print(string1.index("pikachu", 10))
print(string1.index("pikachu", 40))
except ValueError:
print("Not found!")
Output: 31 31 Not found!
Regular Expressions (REGEX)
Regular expressions are used to check if the string matches the pattern. To use regular expressions, we can use the python re package. This module includes the search function which allows to check if a string is contained in another string.
Syntax
search(substring, string)
Example:
from re import search
string = "OH NO! My favorite is Ivysaur!"
substring = "Ivysaur"
substring2 ="Pikachu"
print(search(substring, string).group(0))
print(search(substring2, string))
Output: Ivysaur None
The function returns None if the substring is not found.
This function is very useful if you want to search for a more complex string. The disadvantage is that the function is slower than the other methods listed above. To be used only in specific cases
The __contains__() method
As mentioned above, this method is the implicit version of the in operator. We can therefore use it in the same way as this operator.
Syntax
string1.__contains__('string2')
Example:
string = "OH NO! My favorite is Ivysaur!"
print(string.__contains__("Ivysaur"))
print(string.__contains__("Pikachu"))
Output: True False
The method returns True if the substring was found in the main string, otherwise it returns False.
The force brute method
A simple way is to use brute force by checking whether the substring exists from every possible position in the original string.
I’m not going to detail these methods in this tutorial but I invite you to visit this site:
You will find the python source codes to use this method. 🙂
Summary
As you can see, there are a multitude of ways to check if a string is present in another string. Each method has its own advantages and disadvantages, depending on the projects you are working on.
- The in operator returns True if the substring exists in the string. Otherwise it returns False
- The find method returns the value of the index otherwise it returns the value -1.
- The count function returns the number of occurrences of the substring in the main string.
- The index method returns the index of the string if it is found, otherwise it raises a ValueError Exception.
- You can use the search function of an re module, using regular expressions to determine whether a substring is included in another string.
Here we come to the end of this tutorial. I hope you’ve learned many tricks. Feel free to let me know by writing a comment), it will make me very happy 🙂
Comments
Leave a comment