A substring is a sequence of characters within a String. When writing a Python program you may need to check whether a substring is present inside another string or not, there can be many reasons for that like searching and filtering data, input validation, etc.
To check if a string contains a substring, we can utilise many built-in functions that Python provides us, each one with slightly different functionality.
At the end of this tutorial, you will not only know how to check for substrings but also know a lot of new Python methods, so let’s get started.
Methods to check if a Python string contains a substring
Python has various methods for this check, but we’ll be using those that are easy to implement and require only a few lines of code.
The following are the methods in Python to check if a string contains other substrings.
- Using find() method
- Using in operator
- Using count() method
- Using index() method
- Using operator.contains() method
Now, let’s examine each of them separately, along with their respective syntax and examples.
Using the find() Method
The find() method is used to check whether a string contains a particular substring or not. It takes a substring as an argument and if the string contains that particular substring, it returns the starting index of the substring else it returns -1.
Syntax:
string.find(substring)
Here, substring is the string you want to search, and string is the string object in which you want to search.
Example:
str="Safa Mulani is a student of Engineering discipline."
sub1="Safa"
sub2="Engineering"
print(str.find(substring1))
print(str.find(substring2))
Output:
0
28
Using the ‘in’ operator
The ‘in’ operator checks for the presence of a substring within a string by returning either True or False. If the substring is present it returns True else it returns False.
Syntax:
substring in string
Example:
str="Safa Mulani is a student of Engineering discipline."
sub1="Safa"
sub2="Done"
print(sub1 in str)
print(sub2 in str)
Output:
True
False
Using the count() Method
The count() method checks for the occurrence of a substring in a string. If the substring is not found in the string, it returns 0.
Syntax:
string.count(substring)
Example:
str="Safa Mulani is a student of Engineering discipline."
sub1="Safa"
sub2="student"
sub3="Done"
print(str.count(sub1))
print(str.count(sub2))
print(str.count(sub3))
Output:
1
1
0
Using the index() Method
This method can also check for a substring’s presence in a string. If the substring is not present in the string then it doesn’t return any value, rather it generates a ValueError.
Syntax:
string.index(substring)
Example:
str = "Safa is a Student."
try :
result = str.index("Safa")
print ("Safa is present in the string.")
except :
print ("Safa is not present in the string.")
Output:
Safa is present in the string.
Using the operator.contains() Method
We can also use the operator.contains() method of the “operator” module to check for substrings. It takes a string and a substring as an argument and returns a boolean True if the substring is contained in that string, else it returns False.
Syntax:
operator.contains(string, substring)
Example:
import operator
str = "Safa is a Student."
if operator.contains(str, "Student"):
print ("Student is present in the string.")
else :
print ("Student is not present in the string.")
Output:
Student is present in the string.
Conclusion
In this tutorial, we explored five ways to check if a string contains a substring. Each method offers its own advantages and suitability for different scenarios. Among them, the best way to check whether a Python string contains another string is to use for loop
, as it is easily understandable by beginners, more readable and does not require any additional methods.
With this knowledge, you can efficiently check if string contains a substring in Python programming language. Additionally, there is another method, string.__contains__(), which provides a more efficient way of performing this check. For a detailed tutorial specifically covering this method, you can read “Python String Contains: Check if a String Contains a Substring“. Hope you have enjoyed reading the content.
Reference
https://stackoverflow.com/questions/3437059/does-python-have-a-string-contains-substring-method