Python contains method – Python String contains() Method with Examples

Python contains method: In this post, we’ll see how to determine whether one Python string contains another.

How can we achieve it so easily?

String contains() Method in Python

Contains method python: The String._contains_() method determines whether one string contains another string. It returns True if the target string is found in the string object, and False otherwise.

This can also be used as a class method with two arguments.

The String._contains_() method is case-sensitive.

This function will take two strings and return true or false if one of them belongs to the other. This method’s return type is thus boolean, so it will either return True or False.

Syntax:

variable = stringobject.contains(anotherstring)

Example1

Approach:

  • Give the string as static input and store it in a variable.
  • Check if the given string contains a substring “btechgeeks” using theĀ  __contains__() method and print it.
  • The Exit of the Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "good morning btechgeeks"
# Check if the given string contains a substring "btechgeeks" using the
# __contains__() method and print it
print(gvn_str.__contains__("btechgeeks"))

Output:

True

Example2

Approach:

  • Give the string as static input and store it in a variable.
  • Check if the given string contains the given substring using the __contains__() method and if conditional statement.
  • If it is true, then print “The given string contains the given substring”.
  • Else print “The given string does NOT contain the given substring”.
  • The Exit of the Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "hello this is btechgeeks"
gvn_substr = "hello"
# Check if the given string contains the given substring using the
# __contains__() method and if conditional statement.
if (gvn_str.__contains__(gvn_substr)):
    # If it is true, then print "The given string contains the given substring"
    print("The given string contains the given substring")
else:
    # Else print "The given string does NOT contain the given substring"
    print("The given string does NOT contain the given substring")

Output:

The given string contains the given substring

Example3: String._contains_() method is case-sensitive.

# Give the string as static input and store it in a variable.
gvn_str = "hello this is btechgeeks"
gvn_substr = "HELLO"
# Check if the given string contains given substring using the
# __contains__() method and if conditional statement.
if (gvn_str.__contains__(gvn_substr)):
    # If it is true, then print "The given string contains the given substring"
    print("The given string contains the given substring")
else:
    # Else print "The given string does NOT contain the given substring"
    print("No, the given string does NOT contain the given substring")

Output:

No, the given string does NOT contain the given substring

Explanation:

Because this method is case-sensitive, the sub-string "HELLO" will not be accepted.

Python contains() Function as a Class method

Contains method in python: This can also be used as a class method on the str class, with two arguments instead of one.

This is similar to the last example, but we call it as a Class method on the String class. If string1 contains string2, this function will return True; otherwise, it will return False.

Syntax:

variable = str.contains(string1, string2)

Example:

print(str.__contains__("good morning btechgeeks", "good"))

Output:

True