Checking whether a String Contains a Set of Characters in python
In this tutorial, we will learn how to examine or check whether a string contains any set of characters or a substring and if it contains, we will learn how to know the occurrence of a particular character. Python, a user-friendly language, provides several built-in functions to check the presence of a set of characters in a string. Now, Let us learn various techniques to determine the substring present in a string. Here, we will see different methods to achieve our target, and further, we will see how to implement this through an example.
Following are the methods available in Python to examine the presence of a string containing a set of characters or a substring.
1. By means of the "in" operator available in Python
The easiest and quickest method to examine if a substring is present or not in a string is through the application of the "in" operator. This operator yields true if the character is present in a string. Otherwise, it yields false.
Python in method takes two arguments. One resides on the left-hand side, and the other resides on the right-hand side. It is sensitive to cases, so it treats lower and upper case characters differently.
Example 1:
#Python program to check if a substring is present in a larger string or not
str="bonjour, Bonne nuit, bonsoir World, chat, monsieur, chieval"
print ("World" in str)
Output:

Explanation:
In the above example, we have checked the presence of ‘World’ in the string through in keyword. It yields output as true as 'world' in the provided string.
Example 2:
#Python program to check whether a substring is present in a string or not
str="bonjour, Bonne nuit, bonsoir World, chat, chieval"
print ("hello" in str)
Output:

Explanation:
In the above example, we have checked the presence of ‘World’ in the string through in keyword. It yields output as false as ‘world’ is not present in the provided string.
Example 3:
This is another example of understanding the working of in method in python language.
# Python 3 code to understand how to
# Check substring present in a string
# through the means of in operator
# Initialization of the string
test_str1 = "this_is_for_you"
# using in method to check the presence of substring
print ("Does for exists in this_is_for_you ? : ")
if "for" in test_str1 :
print ("Yes, String is found")
else :
print ("No, String is not found")
Output:

Explanation:
In the above example, We have tried to check the presence of substring ‘for’ in the string test_str1 ‘this_is_for_you’ using the ‘in’ method. Since the word for was present in the provided string, we received the output saying yes, a string is found.
2. By means of str. find() method.
The above method is usually taken into account to obtain used the lowermost index at which the string occurs but also returns -1. If the string is not present, it yields -1 as output. Thus, the string is present if the returned value is greater than 0. Otherwise, it is not present.
Example:
# Python 3 code to understand how to
# check the presence of a substring in a string
# using str.find() method
# initialization of string
test_str1 = " this_is_for_you "
# using str.find() to test the presence of
# for substring
res1 = test_str1.find("for")
if res1 >= 0:
print (" for is present in this_is_for_you ")
else :
print (" for is not present in this_is_for_you ")
Output:

Explanation:
In the above example, We have tried to check the presence of substring ‘for’ in string ‘this_is_for_you’ using the str. find() method. Since the word for was present, we received the output saying ‘for is present in the string’.
3. By means of the str. index()
To perform the analogous task, this method can be used. It differs from the str. find() method as it doesn't return any value like the former. Instead of returning a value, it returns a ValueError if the string is not present. Thus catching the exception is the technique of checking for the presence of a substring in a string.
Example:
# Python 3 code to demonstrate
# checking substring in string
# using str.index()
# initializing string
test_str = "this_is_for_you"
# using str.index() method to test the presence of
# moon substring
try :
res = test_str.index("moon")
print ("moon exists in ")
except :
print ("moon does not exists in this_is_for_you")
Output:

Explanation:
In the above example, we have tried to check the presence of substring ‘moon’ in string ‘this_is_for_you’ using the str. index () method. Since the word moon was not present in the provided string, we received the output saying ‘moon does not exist in this_is_for_you’.
4. By means of the operator.contains() method
This is a less popular method to examine whether a substring is present in a string or not. This method is also operative in completing the job of inspecting a string in a string.
# Python 3 code to understand how to
# check the presence of a substring in string
# using operator.contains() method
import operator
# initialization of a string
test_str1 = " this_is_for_you "
# using operator. contains() to test the presence of
# for substring
if operator.contains(test_str1, "for"):
print ("for is present in this_is_for_you ")
else :
print ("for is not present in this_is_for_you ")
Output:

Explanation:
In the above example, using an operator, we have tried to check the presence of substring 'for' in string 'this_is_for_you'.contains() method. Since the word for was present in the string, we received the output in the above manner.
5. By means of String.count() function:
This function is considered to keep track or count the occurrence of an element in the string. It can be very useful in checking the occurrence of a particular element in a large string. If the searched string exists in the larger string, the function returns some number, and if it doesn't exist, 0 is returned by the function.
Example:
# Python 3 code to understand how to
# check the presence of a substring in a string
# using String.count() method
# initialization of string
test_str1 = " this_is_for_you "
# Substring whose presence is to be checked
temp1 = "for"
# using string.count to test the presence of ‘for’ substring
ans1 = test_str1.count(temp1)
# Printing our result
if ans1:
print ("for is present in this_is_for_you ")
else :
print ("for is not present in this_is_for_you ")
Output:

Explanation:
In the above example, we have tried to check the presence of substring ‘for’ in the string ‘this_is_for_you’ using string. Count method. Since the word was present, we received the output in the above manner.
6. By means of re.search() function:
The regular expressions in Python are popularly used to match the patterns.
This function is taken into account to find out the pattern lying in the string.
A sub-string can be taken into account as a pattern to search in string.
Example:
# code is written in Python version 3 to understand how to
# check the presence of a substring in the string
# by means of re.search() function
import re
# initialization of the string
test_str1 = "this is for you"
# Substring whose presence is to be checked
temp1 = "is"
# by means of re.search method to test for substring
ans1 = re.search(temp1, test_str1)
# Printing the output
if ans1:
print ("is present in this is for you")
else :
print ("is is not present in this is for you")
Output:

Explanation:
In the above example, we have tried to check the presence of substring ‘is’ in the string ‘this_is_for_you’ using Python's regular method called re. search. Since the word is was present in the string, we received the above output.
Summary:
In this module, we studied how to check the presence of a substring or a character in a larger string in a python programming language. We saw various techniques to check this. We can do this by applying the following five methods: in method, regular expressions. search() method, str. find() method, operator.contains() method, string.count() function. We saw examples for each of the above methods to understand the implications in a better way.