Introduction
Python's string class has a __contains__() function that we can use to check whether it contains another string or not.
Python string contain() method
The Python string __contains__() is an instance method and returns a boolean value of True or False depending on whether the string object contains the specified string object. Note that the Python string contain() method is case sensitive. Let's look at a simple example of the string __contains__() method.
s = 'abc'
print('s contains a =', s.__contains__('a'))
print('s contains A =', s.__contains__('A'))
print('s contains X =', s.__contains__('X'))Output:
s contains a = True
s contains A = False
s contains X = FalseWe can also use the __contains__() function as a method of the str class.
print(str.__contains__('ABC', 'A'))
print(str.__contains__('ABC', 'D'))
Output:
True
FalseCheck if Python string contains substring
Let's look at another example where we ask the user to enter both strings and check if the first string contains a string or substring of the second string.
input_str1 = input('Please enter first input string\n')
input_str2 = input('Please enter second input string\n')
print('First Input String Contains Second String? ', input_str1.__contains__(input_str2))Output: Please enter first input string JournalDev is Nice Please enter second input string Dev First Input String Contains Second String? True









