Python String contains() | Explained

Python supports multiple methods to work with strings, such as find(), format(), islower(), etc. All these functions serve specific operations/tasks on the given string. In Python, the “str.__contains__()” is a case-sensitive method used to check the existence of a particular string in the given string. 

This post will present an in-depth tutorial on the Python string contains() method:

How to Use String.__contains__() in Python?

It is a built-in string function that retrieves the boolean values “True” or “False” based on the existence of the specified string in the given string.

Let’s comprehend the usage of the string “contains()” method using the following examples.

Example 1: Using str.__contains__() to Check the Specified String

The “str.__contains__()” method is used in the below code to check the specified string:


val = 'itslinuxfoss' 
print("Given String contains 'l' =", val.__contains__('l')) 
print("\nGiven String contains 't' =", val.__contains__('t')) 
print("\nGiven String contains 'foss' =", val.__contains__('foss'))
  • First, the string named “val” is initialized.
  • The “val.__contains__()” method accepts a substring “l” and checks its existence in the input string.
  • If the string contains the specified value “l”, then the boolean value “True” will be returned else, “False”.


The given string contains the specified substrings.

Example 2: Using str.__contains__() as a String Class Method

The below code uses the “str.__contains__()” function as a string class method to check a particular substring in the given string:


print(str.__contains__('Python', 'P')) 
print(str.__contains__('Python', 'L'))
  • The “str.__contains__()” accepts two arguments. The first argument takes the input string, and the second argument takes the specified substring.
  • If the particular substring is present/exists in the given string, the boolean “True” will be returned.
  • If the particular string is not present/exists in the given string, the boolean “False” will be returned.


The character “P” is present in the given string, but the character “L” is not present.

Example 3: Using str.__contains__() to Check the User Input String

The below code takes the input from the user and checks its presence in the given string:


val_1 = input('Enter the first string: ') 
val_2 = input('Enter the second string: ') 
print('Checking Second String in First String: ', val_1.__contains__(val_2))
  • The “input()” function takes the input string from the user.
  • The second string, “val_2”, is the specified string whose existence needs to be checked in the first string, “val_1”.
  • The “str.__contains__()” method accepts the second string as an argument and checks its existence in the first string.


The boolean “True” shows that the substring is present in the input string.


The Python string “str.__contains__()” method is used to check the presence/existence of the specified string in the given string. The “str__contains__()” method returns the boolean value “True” when the particular string exists in the given string. Otherwise, it returns “False”. This post provided a comprehensive guide to the Python string contains() method using numerous examples.