Methods to check if a python string contains a substring

In this python tutorial, we look at how you can check if a python string contains a substring. We look at the various methods and explain their use cases in detail.

Table of Contents: check if a python string contains a substring

  • Why check if a python string contains a substring?
  • Using the in operator
  • Using String Methods
  • Limitations and Caveats

Why check if a python string contains a substring?

We check if a python string contains a substring for multiple reasons, however, it's most commonly used in conditional statements.

A particular code is run in this case. Another common use is to find the index of the substring in the string.

It is most likely that you have come across the contains function in other programming languages. Python also supports the __contains__ method. It also supports a few faster and more readable methods to check if a python string contains a substring. We are going to explore these below.

Using the 'in' operator:

The in operator is the easiest and pythonic way to check if a python string contains a substring.

The in and not in are membership operators, they take in two arguments and evaluate if one is a member of the other. They return a boolean value. This is an efficient alternative to the __contains__ method and can also be used to check if an item exists in a list.

The in method can only be used to check if a python string contains a substring. In case you are looking to return the index of the substring, the next solution provides that.

Syntax of in:
substring in string

The syntax for not in is the same.

Code to check if python string contains a substring:
if "Hire" in "Hire the top freelancers":
    print("Exists")
else:
    print("Does not exist")

#Output - Exists

The in operator is case sensitive, and the above code would've returned false if the substring was "hire" and hence is a good practice to use it with the .lower() method.

This method converts the string to lower case. As strings are immutable, this would not affect the original string.

if "hire" in "Hire the top freelancers".lower():
    print("Exists")
else:
    print("Does not exist")

#Output - Exists

Using String Methods:

Python comes with a few string methods that can be used to check if a python string contains a substring. Out of the different methods, we shall look at the find() and Index() methods.

These methods find and return the index of the substring. However, they have a few cons, which we will discuss in detail.

Using index()

The string.index() method returns the starting index of the substring passed as a parameter.

However, a major con is that it returns a ValueError in case the substring does not exist. We can solve this by using a Try Except.

Syntax of index():
string.index(value, start, stop)

Here string refers to the python string and value is the substring.

The syntax also contains two optional parameters start and stop. These take in index values and help you look for a substring within a specific index range.

Code using index():
try:
    "Hire the top freelancers".index("Hire")
except ValueError:
    print("Does not exist")
else:
    print(sting.index(sti))

#Output = 0

index() is case sensitive, ensure you use the .lower() function to avoid bugs.

try:
    "Hire the top freelancers".lower().index("hire")
except ValueError:
    print("Does not exist")
else:
    print(sting.index(sti))

#Output = 0

Using find():

The string.find() is another method that can be used to check our query. Similar to the index() method, find() also returns the starting index of the substring. However, find() returns -1 in case the substring does not exist. -1 is the negative index of the leftmost character.

Syntax of find():
string.find(value, start, end)

The syntax and parameters of find() are the same as index()

Code using find():
if "Hire the top freelancers".find("Hire") != -1:
    print("Hire the top freelancers".find("Hire"))
else:
    print("Does not exist")

And again, find() is also case sensitive and the .lower() method must be used.

if "Hire the top freelancers".lower().find("hire") != -1:
    print("Hire the top freelancers".find("Hire"))
else:
    print("Does not exist")

Check if a python string contains a substring - Limitations and Caveats:

  • Remember to use the .lower() methods as all the methods are case-sensitive.
  • Which uses the index() method to ensure it is placed inside an try and except condition.