Built with Tutify.io

    AIStudyLabs

    Python String Contains

    Python String Contains

    In this article, we will discuss different ways to check whether the given substring exists in the string or not.

    <a id="h-1-quick-examples-of-string-contains"></a>

    1. Quick Examples of string contains

    
    from re import search
    
    # Consider the string
    string = 'welcome to sparkby tutorials!'
    print("Original: ",string)
    
    # Using __contains__
    print("sparkby exists? ",string.__contains__('sparkby'))
    print("python exists? ",string.__contains__('python'))
    
    # Using in operator
    if ("sparkby" in string):
        print("sparkby Exists")
    else:
        print("sparkby doesn't Exists")
    
    # Using not in operator
    if ("sparkby" not in string):
       print("sparkby doesn't Exists") 
    else:
        print("sparkby Exists")
    
    # Using index()
    try:
        string.index("sparkby")
    except ValueError:
        print("sparkby doesn't Exists")
    else:
        print("sparkby Exists")
    
    # Using find()
    if (string.find("sparkby") == -1):
        print("sparkby doesn't Exists")
        
    else:
        print("sparkby Exists")
    
    # Using search()
    if (search('sparkby', string)):
        print("sparkby Exists")
    else:
         print("sparkby doesn't Exists")
    

    <a id="h-2-python-string-in-operator"></a>

    2. Python String in operator

    in operator in Python is used to check whether the given substring exists in a string or not. If it exists, true is returned, Otherwise False is returned.

    <a id="h-2-1-syntax-string-in-operator"></a>

    2.1 Syntax - String in operator

    The following is the syntax of the String in operator.

    
    substring in string
    

    It can be possible to use in operator inside if-else block.

    
    if (substring in string):
        print("Exists")
    else:
        print("Doesn't Exists")
    

    Here, string is the actual string and substring is the string to be checked within the string.

    <a id="h-2-2-return-of-in-operator"></a>

    2.2 Return of in operator

    1. True, if the substring is present in the string and statements inside if block will be executed.
    2. False, if the substring is not present in the string and statements inside else block will be executed.

    <a id="h-2-3-python-string-in-operator-example"></a>

    2.3 Python String in operator example

    Let's check two substrings exists in the string or not.

    
    # Consider the string
    string = 'welcome to sparkby tutorials!'
    print("Original: ",string)
    
    # Check whether 'sparkby' exists in the string or not.
    if ("sparkby" in string):
        print("sparkby Exists")
    else:
        print("sparkby doesn't Exists")
    
        # Check whether 'python' exists in the string or not.
    if ("python" in string):
        print("python Exists")
    else:
        print("python doesn't Exists")
    
    # Output:
    # Original:  welcome to sparkby tutorials!
    # sparkby Exists
    # python doesn't Exists
    

    Explanation:

    We can see that the substring-"sparkby" exists in the string (If block is executed) and substring-"python" doesn't exists in the string (Else block is executed).

    <a id="h-3-python-string-not-in-operator"></a>

    3. Python String not in operator

    not in operator in Python is used to check whether the given substring exists in a string or not. If it doesn't exists, true is returned, Otherwise False is returned. It is opposite to in operator.

    <a id="h-3-1-syntax-string-not-in-operator"></a>

    3.1 Syntax - String not in operator

    The following is the syntax of the String not in operator.

    
    substring not in string
    

    It can be possible to use not in operator inside if-else block.

    
    if (substring not in string):
        print("Doesn't Exists")
    else:
        print("Exists")
    

    Here, string is the actual string and substring is the string to be checked within the string.

    <a id="h-3-2-return-of-not-in-operator"></a>

    3.2 Return of not in operator

    1. False, if the substring is present in the string.
    2. True, if the substring is not present in the string.

    <a id="h-3-3-python-string-not-in-operator-example"></a>

    3.3 Python String not in operator example

    Let's check two substrings exists in the string or not.

    
    # Consider the string
    string = 'welcome to sparkby tutorials!'
    print("Original: ",string)
    
    # Check whether 'sparkby' exists in the string or not.
    if ("sparkby" not in string):
       print("sparkby doesn't Exists")
        
    else:
        print("sparkby Exists")
    
        # Check whether 'python' exists in the string or not.
    if ("python" not in string):
      print("python doesn't Exists")
        
    else:
      print("python Exists")
    
    # Output:
    # Original:  welcome to sparkby tutorials!
    # sparkby Exists
    # python doesn't Exists
    

    Explanation:

    We can see that the substring-"sparkby" exists in the string (Else block is executed) and substring-"python" doesn't exists in the string (If block is executed).

    <a id="h-4-python-string-contains"></a>

    4. Python String contains

    contains() is an instance method which return true, if the substring exists, otherwise false is returned.

    <a id="h-4-1-syntax-string-contains"></a>

    4.1 Syntax - String contains()

    The following is the syntax of the String contains() method

    
    string.__contains__(substring)
    

    Here, string is the actual string and substring is the string to be checked within the string.

    <a id="h-4-2-return-of-string-contains"></a>

    4.2 Return of String contains()

    1. True, if the substring is present in the string.
    2. False, if the substring doesn't not present in the string.

    <a id="h-4-3-python-string-contains-example"></a>

    4.3 Python String contains() example

    Let's check two substrings exists in the string or not.

    
    # Consider the string
    string = 'welcome to sparkby tutorials!'
    print("Original: ",string)
    
    # Check whether 'sparkby' exists in the string or not.
    print("sparkby exists? ",string.__contains__('sparkby'))
    
    # Check whether 'python' exists in the string or not.
    print("python exists? ",string.__contains__('python'))
    
    # Output:
    # Original:  welcome to sparkby tutorials!
    # sparkby exists?  True
    # python exists?  False
    

    Explanation:

    We can see that the substring-"sparkby" exists in the string and substring-"python" doesn't exists in the string.

    <a id="h-5-python-string-index"></a>

    5. Python String index()

    index() method returns the index position of the first character of the substring if it exists in the string. If not exists, ValueError exception is raised. So we can control the program with try-except blocks.

    So if the substring exists, Else block is executed, otherwise the error message specified inside the except block is returned.

    <a id="h-5-1-syntax-string-index"></a>

    5.1 Syntax - String index()

    The following is the syntax of the String index method

    
    # index() syntax
    string.index(substring)
    
    
    try:
        string.index("python")
    except ValueError:
        print("Doesn't Exists")
    else:
        print("Exists")
    

    Here, string is the actual string and substring is the string to be checked within the string.

    <a id="h-5-2-parameters-of-string-index"></a>

    5.2 Parameters of String index()

    It will take only substring as a parameter.

    <a id="h-5-3-return-of-string-index"></a>

    5.3 Return of String index()

    1. If the substring is not present, valueError Exception is raised.
    2. If the substring is present, substring first character index position is returned.

    <a id="h-5-4-python-string-index-example"></a>

    5.4 Python String index() example

    Let's check two substrings exists in the string or not.

    
    # Consider the string
    string = 'welcome to sparkby tutorials!'
    print("Original: ",string)
    
    # Check whether 'sparkby' exists in the string or not.
    try:
        string.index("sparkby")
    except ValueError:
        print("sparkby doesn't Exists")
    else:
        print("sparkby Exists")
    
        # Check whether 'python' exists in the string or not.
    try:
        string.index("python")
    except ValueError:
        print("python doesn't Exists")
    else:
        print("python Exists")
    
    # Output:
    # Original:  welcome to sparkby tutorials!
    # sparkby Exists
    # python doesn't Exists
    

    Explanation:

    We can see that the substring-"sparkby" exists in the string and substring-"python" doesn't exists in the string.

    <a id="h-6-python-string-find"></a>

    6. Python String find()

    find() method returns the index position of the first character of the substring if it exists in the string. If not exists, -1 is returned.

    <a id="h-6-1-syntax-string-find"></a>

    6.1 Syntax - String find()

    The following is the syntax of the String find() method

    
    # find() syntax
    string.find(substring)
    

    Here, string is the actual string and substring is the string to be checked within the string.

    <a id="h-6-2-parameters-of-string-find"></a>

    6.2 Parameters of String find()

    It will take only substring as a parameter.

    <a id="h-6-3-return-of-string-find"></a>

    6.3 Return of String find()

    1. If the substring is not present, -1 is returned.
    2. If the substring is present, substring first character index position is returned.

    <a id="h-6-4-python-string-find-example"></a>

    6.4 Python String find() example

    Let's check two substrings exists in the string or not.

    
    # Consider the string
    string = 'welcome to sparkby tutorials!'
    print("Original: ",string)
    
    # Check whether 'sparkby' exists in the string or not.
    if (string.find("sparkby") == -1):
        print("sparkby doesn't Exists")
        
    else:
        print("sparkby Exists")
    
        # Check whether 'python' exists in the string or not.
    if (string.find("python") == -1):
      print("python doesn't Exists")
      
    else:
        print("python Exists")
    
    # Output:
    # Original:  welcome to sparkby tutorials!
    # sparkby Exists
    # python doesn't Exists
    

    Explanation:

    We can see that the substring-"sparkby" exists in the string and substring-"python" doesn't exists in the string.

    <a id="h-7-python-string-search"></a>

    7. Python String search()

    search() method is available in re module which will check for substring present in the string or not.

    search() method will return None, if the substring doesn't exists in the string.

    <a id="h-7-1-syntax-for-string-search"></a>

    7.1 Syntax for String search()

    The following is the syntax of the String search() method.

    
    if (search(substring, string)):
        print("Exists")
    else:
         print("Doesn't Exists")
    

    Here, string is the actual string and substring is the string to be checked within the string.

    <a id="h-7-2-parameters-of-string-search"></a>

    7.2 Parameters of String search()

    It will take only substring as first parameter and actual string as the second parameter.

    <a id="h-7-3-return-of-string-search"></a>

    7.3 Return of String search()

    1. If the substring is not present, Else block is executed.
    2. If the substring is present, If block is executed.

    <a id="h-7-4-python-string-search-example"></a>

    7.4 Python String search() example

    Let's check two substrings exists in the string or not.

    
    from re import search
    
    # Consider the string
    string = 'welcome to sparkby tutorials!'
    print("Original: ",string)
    
    # Check whether 'sparkby' exists in the string or not.
    if (search('sparkby', string)):
        print("sparkby Exists")
    else:
         print("sparkby doesn't Exists")
    
         # Check whether 'python' exists in the string or not.
    if (search('python', string)):
        print("python Exists")
    else:
         print("python doesn't Exists")
    
    # Output:
    # Original:  welcome to sparkby tutorials!
    # sparkby Exists
    # python doesn't Exists
    

    Explanation:

    We can see that the substring-"sparkby" exists in the string and substring-"python" doesn't exists in the string.

    <a id="h-8-conclusion"></a>

    8. Conclusion

    We seen 7 ways to check whether the substring contains in the string or not. Make sure you have to import re module while using search() method. While() using index() method, Use try-except blocks.

    <a id="h-related-articles"></a>

    Related Articles