Check If Python String Contains Substring


In this python tutorial, we will look at different ways to check if a python string contains substring. Additionaly we will also find the index of the substring in the string.

    Table Of Contents

  1. 4 ways to check if string contains a substring
    1. Using in keyword
    2. Using index() method
    3. Using find() method
    4. Using re.search() method
  2. 2 Ways to find index of substring in string
    1. Find substring using find() method
    2. Find substring using index() method
  3. Conclusion

python string contains substring

4 ways to check if string contains a substring

Python provides 4 different ways by which you can check if a string contains a substring or not.

The following methods return a boolean value (True or False) based on the result.

1. Using in Operator

The in operator is used to check if certain data is part of a data structure or not. You can use it to check if a string contains a substring or not.

Let's see how can we use the in operator.

str = "TutorialsTonight"
substr = "Tutorial"

if substr in str:
    print("Substring found!")
else:
    print("Substring not found!")

Output:

Substring found!

One thing to note is that in operator is not Null-safe. It will throw an error if you try to use it on a null value. To escape this error, first, check if the given string is not null.

str = None
substr = "Tutorial"

if str is not None and substr in str:
    print("Substring found!")
else:
    print("Substring not found!")

Output:

Substring not found!

2. Using index() Method

String class provides a method index() which returns the index of the first occurrence of a substring in a string. If the substring is not found, it returns -1.

We can use this method to check if a string contains a substring or not.

This method throws an error if the substring is not found. So always use the try-except block to handle this error.

str = "TutorialsTonight"
substr = "Tutorial"

try:
    if str.index(substr) != -1:
        print("Substring found!")
except ValueError:
    print("Substring not found!")

Output:

Substring found!

3. Using find() Method

find() is also a string method in python. This method is pretty much the same as the index() method. The only difference is that the find() method handles the None value. If the string is null, it returns -1.

The basic function is the same, it returns the index of the first occurrence of a substring in a string and returns -1 if the substring is not found.

Let's see how to decide if a string contains a substring or not using the find() method.

str = "TutorialsTonight"
substr = "Tutorial"

if str.find(substr) != -1:
    print("Substring found!")
else:
    print("Substring not found!")

Output:

Substring found!

The re module (regular expression) provides a method search() which is used to search a string for a pattern using regular expressions.

If the pattern is found, it returns a match object whose boolean value is always True. If the pattern is not found, it returns None.

This method can also be used to check if the python string contains a substring.

from re import search

str = "TutorialsTonight"
substr = "Tutorial"

if search(substr, str):
    print("Substring found!")
else:
    print("Substring not found!")

Output:

Substring found!

2 Ways to find index of substring in string

Above we have seen 4 ways that determine whether a string contains a substring or not. But sometimes just knowing is not enough, we need to know the index of the substring in the string.

Here are 2 ways using which we can find the index of a substring in a string.

1. Find substring using find() Method

As we know, the find() method returns the index of the first occurrence of a substring in a string. If the substring is not found, it returns -1.

str = "Learn Python Programming"
substr = "Python"

index = str.find(substr)

print("Index of substring is: ", index)

Output:

Index of substring is:  6

2. Find substring using index() Method

The index() method also works the same as the find() method. The only difference is that index() method throws an error if the substring is not found.

str = "Learn Python Programming"
substr = "Python"

try:
    index = str.index(substr)
except ValueError:
    print("Substring not found!")

print("Index of substring is: ", index)

Output:

Index of substring is:  6

Conclusion

We have seen 4 ways to check if a string contains a substring or not with examples.

Additionally, we also have seen 2 ways to find the index of a substring in a string.