Does Python have a string contains substring method?

Posted in

Does Python have a string contains substring method?

Vinay Khatri
Last updated on November 6, 2022

    Unfortunately, Python does not have any specific contains or substring method to check if a string contains a substring. But Python offers many keywords and methods that can help us to find if a string is a substring of a given string.

    Way 1: Using Python in Operator

    The easiest way to check if a string contains a given substring is by using the Python in operator or keyword.

    The in is a reserved keyword in Python that returns a boolean value True or False based on whether the string contains the substring or not.

    The in operator returns True if the string contains the substring else it returns False .

    Example

    string = "Techgeekbuzz"
    
    substring = "geek"
    
    #check if string contains substring
    
    print(substring in string)

    Output

    True

    Way 2: Using the Python string find() method

    The Pythons string supports the find method, which can also tell whether the string contains a given substring. The find() method accepts the substring as an argument value and returns the starting index number of the string from where the substring is starting, otherwise, it returns -1.

    Example

    string = "Techgeekbuzz"
    
    substring = "geek"
    
    #check if string contains substring
    
    print(string.find(substring))

    Output

    4

    In this example, the substring “ geek” is present in the string “ techgeekbuzz” , and it starts from index 4; that’s why the find() method returns 4 as an output.

    Way 4: Using the Python string count() method

    As the method name suggests using the count() method, we can count the occurrence of a substring in a given string.

    If the occurrence count of the substring is greater than 0, we can tell that the string contains the substring; if the count remains 0 this means the string does not contain the substring.

    Example

    string = "Techgeekbuzz"
    
    substring = "geek"
    
    #check if string contains substring
    
    if string.count(substring)0:
    
        print("The string does not contain the given substring")
    
    else:
    
        print("The string contains the given substring")

    Output

    The string contains the given substring

    Way 5: Using the Python Regular Expression

    Regular Expression is the most efficient and widely used way to match patterns or strings in any programming language. Python also supports built-in module re for regular expressions operations. We can use the re method to check if a string contains the substring.

    The Python’s re module supports a search() method that we can use to check the string’s substring.

    If the substring is present in the string, the search method returns a re.Match object otherwise, it returns None.

    Example

    import re
    
    
    
    string = "Techgeekbuzz"
    
    substring = "geek"
    
    # #check if string contains substring
    
    if re.search(substring, string):
    
        print("The string contains the given substring")
    
    else:
    
        print("The string does not contains the given substring")

    Output

    The string contains the given substring

    Way 6: Define a Custom contains() method

    We can also create a custom class String that accepts a string value as an initializer object and have a contains() method which returns a boolean value True if the string contains the substring otherwise, returns False.

    Example

    class String:
    
         #initialize the string value
    
        def __init__(self,value):
    
            self.string = value
    
        def contains(self, substring):
    
            return substring in self.string
    
    
    
    #initialize the object
    
    string = String("Techgeekbuzz")
    
    
    
    substring = "geek"
    
    
    
    print(string.contains(substring))

    Output

    True

    Bonus:  Python string __contains__() method

    If we list out all the supported methods and properties of an str object in Python there, we can see that the str object supports the __contains__() method.

    python contains method

    Generally, we do not use this method to find the substring in a given string because it is semantically a private method.

    But we can use this method to find if a string contains the substring. The __contains__() method also returns a boolean value like in operator, actually, the in operators invoke the __contians__ method behind the scenes. This means both in operator and __contains__ method are the same.

    Example

    string = "Techgeekbuzz"
    
    substring = "geek"
    
    print(string.__contains__(substring))

    Output

    True

    Tags:
    python

    Leave a Comment on this Post

    0 Comments