in this post, I am going to help you check if the string contains substring with example. The string is the main package of python. We will check a word or character is found or not in a python string.
This is the most common operations that developers use on strings is to check whether a string contains some other string.
Python has an inbuilt method like find, index, etc. These methods help to find the substring and character in the source string.
The following are the methods in Python to check if a string contains another substring:
- Find Substring Using
in
operator - By using
find()
method - Check Substring Using
not in
method - Find Substring Using
str.index()
method - Check Substring Using
operator.contains()
method
Python String Contains Example
Let’s create a sample example to check a string contains a substring.
in Operator
The python has in
operator to check character or substring into the string. The in operator returns the True value when the substring exists in the string. Otherwise, it returns false. You can use the in
operator.
It also works with lists for checking if an item exists in a list. it’s not null-safe.
Syntax:
substring in source_string
Example Using python in Operator
s = "This is a boy" "is" in s True "Is" in s False
It would print True if 'is'
found in the source string otherwise False.
Output:
True
How To use python find() method
The find()
function in Python can be used to check for substrings in a string. The find()
function in Python is used to locate the index of a substring within a string.
Syntax:
string.find(substring)
Example Using python find()
s = "This is a boy." if s.find("is") == -1: print("No 'is' here!") else: print("Found 'is' in the string.")
Output:
Found 'is' in the string.
Python not in operator
Let’s see a not in
operator in Python with an example.
Python contains String using not in Operator
s = "I love python programming." if "love" not in s: print('is not exist in the String.') else: print('It exists.')
Output:
It exists.
Python index() method
Python String index()
is a function that returns the index of a substring. The index()
method finds the first occurrence of the substring into the source string. The index() method raises the exception if the value is not found.
An index() method is almost the same as the find() method, the only difference is that find() method returns -1 if that value is not found.
Syntax:
string.index(substring)
s = "I love python programming." isexist = s.index('love') print(isexist)
Output:
2
The python index returns the lowest index of the found substring. If substring doesn’t exist inside the string, it raises a ValueError exception. This method is useful when you want to know the index position of the substring.
operator.contains() method
You can also use operator.contains() method to check a string contains a substring.
Syntax:
operator.contains(string,substring)
import operator s = "I love python programming." if operator.contains(s, "python"): print ("python is found.") else : print ("python is not found.")
Output:
python is found.