Checking whether a string contains a substring is a basic Python string manipulation operation, but it’s so useful that every single fully-fledged website implements it. For instance, if you signup, you see several prompts suggesting password changes that must contain a certain set of predefined characters, similarly, the website checks if the input email contains the substring “@” for email validation. These operations can be easily done in Python using a few lines of code.
In this tutorial, we will learn how to check whether a string contains another string using four different methods so that you can use them accordingly. let’s get started.
Methods to Check if a Python String Contains a Substring
When you’re working with substrings, you can use the below methods to check whether a string contains a substring in Python
- in Operator
- string.find() Function
- string.index() Function
- Regular Expression
Let’s see them one by one, with syntax and examples.
1. Using the in operator
Using the in operator is the easiest and quickest way to check if a Python string contains a substring. The in operator is used to check data structures for membership in Python. The in operator returns a Boolean value as a response.
Below is the syntax for using the in operator.
Syntax:
substring in string
where the substring is the string you want to check whether it is present in the mentioned string or not.
Example:
fullString = "Codeforgeek"
subString = "code"
if subString.lower() in fullString.lower():
print("Found the substring!")
else:
print("Substring not present in the string!")
Here we have used the lower() method since the comparison is case-sensitive, so by default, it returns false if we search “code” in “Codeforgeek”.
Output:
Found the substring!
The in operator also works well with checking the items in the list.
Also Read: Python if…else Conditional Statement (With Examples)
2. Using the find() function
We can use the find() method provided by Python to check if a substring is present in the string. If the match is not found, it returns -1, otherwise, it returns the left-most index of the substring in the string.
Below is the syntax for using the find() function.
Syntax:
string.find(substring)
where the substring is the string you want to search in the mentioned string.
Example:
fullString = "Codeforgeek"
subString = "code"
if fullString.find(subString) != -1:
print("Found!")
else:
print("Not found!")
This method is also case-sensitive.
Output:
Not found!
If you like to find the position of a string as well along with the existence check, do check out the next approach.
3. Using the index() function
We can use the index method provided by Python to check the starting index of the first occurrence of a substring within a string. If the substring is not found, a ValueError exception is thrown, which needs to be handled with a try-except-else block to avoid a program crash.
Below is the syntax for using the index() function to check if a string contains a substring.
Syntax:
string.index(substring)
where the substring is the string you want to search in the original string.
Example:
fullString = "Codeforgeek"
subString = "code"
try:
fullString.index(subString)
except ValueError:
print("Not found!")
else:
print("Found!")
Here the comparison is also case-sensitive, just like all the above examples, but if you want a case-insensitive comparison, you can use the lower() method first to convert both strings into lowercase before comparing.
Output:
Not found!
This method returns the position of a substring hence use it if you want to find the position of a substring along with its existence.
4. Using the Regular Expression
You can use the search() method provided by the Python re package to perform a regular expression search. This can be useful for complex searches. This method returns a match object if the substring is found, but you can use the bool() method and pass the result as an argument to get the boolean value, which is either True or False, depending on whether the substring is found on the specified string.
Here is the syntax of the search() method to perform the regular expression search.
Syntax:
re.search(substring, string)
where the substring is the string you want to search, and the string can be a string object in which you want to search.
Example:
import re
fullString = "Codeforgeek"
subString = "code"
if re.search(re.escape(subString), fullString, re.IGNORECASE):
print("Found!")
else:
print("Not found!")
Here we have used “re.IGNORECASE” flag to perform a case-insensitive comparison.
Output:
Found!
Read More: Python Keywords – Simply Explained!
Conclusion
In this tutorial, we have learned to find whether a string contains substring in Python programming language using four different string methods: the in operator, the find() function, the index() function, and the regular expression. Among them, the easiest and most readable method is using the in operator. Hope you have enjoyed reading the content.
Reference
https://stackoverflow.com/questions/3437059/does-python-have-a-string-contains-substring-method