You are currently viewing Python String Contains

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

Advertisements

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")

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.

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.

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.

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).

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.

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.

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.

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).

4. Python String __contains__

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

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.

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.

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.

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.

5.1 Syntax – String index()

The following is the syntax of the String index method


# index() syntax
string.index(substring)
# index() with With try-except block.

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.

5.2 Parameters of String index()

It will take only substring as a parameter.

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.

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.

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.

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.

6.2 Parameters of String find()

It will take only substring as a parameter.

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.

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.

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.

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.

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

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

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.

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.