In this post, we are going to learn how to check if string contains substring in Python with code example. The different ways to check a String that contains a substring in Python we will use some inbuilt function.
1. Using string.find() method
In this code example, we are using the string find() method .The string find() method returns the first concurrence of string values if found else returns -1.
Code Example
#Program to Check if string contains substring in Python
string = 'learn string in python'
substring = 'learn'
if string.find(substring) == -1:
print(substring,'not found')
else:
print(substring,'-is found')
Output
learn is found
2. Using Index() to Check if string contains substring
In this example, we are using the string Index() method the string index() method returns the index of the element we are looking for. If the element is not found it raises ValueError exception.
Syntax
index(element,start,end)
- element: The element we are looking for.
- Start: This is an optional parameter, the index for start search.
- End: This is an optional parameter, the index for end search.
Code Example
string = 'learn string in python'
substring = 'learn'
try:
string.index(substring)
except ValueError:
print(substring,'not found')
else:
print(substring,'is found')
Output
learn is found
3. Using In operator
In this example, we are using IN operator to check if the Python string contains a substring. The “IN” operator checks if a string or character exists in another string. If exists, it returns true else returns false.
Code Example
string = 'learn string in python'
substring = 'learn'
if substring in string:
print(substring,'is found')
else:
print(substring,'not found')
Output
learn is found
4. using “Not in” operator
The “not in” operator works opposite to IN operator. if the substring does not exist then it returns true and else returns false. In the case of Not in operator false mean sub-string exist in string.
Code Example
#program to find string contain substring in Python
string = 'learn string in python'
substring = 'learn'
if substring not in string:
print(substring,'is not found')
else:
print(substring,'is found')
Output
learn is found
5. Using Count() method
In this example, we are using the count method. The count() method returns all the concurrence of a substring in the original string. If the occurrence is greater than 0 mean string contains the substring.
Code Example
string = 'learn string in python learn'
substring = 'learn'
count_ocurr = string.count(substring)
print('count of all occurrence of substring =',count_ocurr)
if count_ocurr>0:
print(substring,'is found')
else:
print(substring,'is not found')
Output
count of all occurrence of substring = 2
learn is found
6. Using Case-insensitive way
Often the original string contains the substring in uppercase or lowers so we can check the string contain the substring by ignoring the case. We are converting the string and substring into the lower() cases and find the substring in a string.
Code Example
#Program to find string contain substring in Python
string = 'learn string in python'
substring = 'LEARN'
if substring.lower() in string.lower():
print(substring,'is found')
else:
print(substring,'is not found')
Output
learn is found
7. Regex to find string contains a substring
In this code example, we are using the Regular expression to find a string that contains a substring. We are using the search() method of regular expression, if substring found it return true else return false.
Code Example
#program to find string contain substring in Python
import re
string = 'learn string in python'
substring = 'learn'
if re.search(substring,string):
print(substring,'is found')
else:
print(substring,'is not found')
Output
learn is found
8. Regex Case-sensitive to check string contains a substring
In this code example, we are using the Regular expression the search() method with flags=re.IGNORECASE, if substring found it return true else return false.
Code Example
#Program to find string contain substring in Python
import re
string = 'learn string in python'
substring = 'LEARN'
if re.search(substring,string,flags=re.IGNORECASE):
print(substring,'is found')
else:
print(substring,'is not found')
Output
LEARN is found
Conclusion:
In this post, Python how to check if a string contains a substring. Now, We can easily find if a String contains a substring in Python.