Checking if a string contains a substring is a task that comes up often when you are using any programming language, and if you’re trying to figure out how to do that in Python, you’re in luck. You have many options to check if a Python string contains a substring.

Below are some ways you can check for a substring in Python. Of these, using the operator is the most common when you only need to check the existence of the substring. Still, you may need other information besides a simple Boolean, so the other methods here can be just as valuable.

person coding

Using the Python string contains method

The python str class has a __contains__() method that will check if a python string contains a substring. It will return true if it’s found and will return false if it’s not. You will notice that the method name is wrapped in two underscores. This prevents this method from being overridden by a subclass or accessed accidentally.

You can use it in two ways. The first is to use the contains method of the String object you are checking:

my_string = ‘This is the string being checked.”
my_substring = ‘string’
if (my_string.__contains__(my_substring)):
    print(“String contains substring!”)
else:
    print(“String does not contain substring”)
The Complete Python Bootcamp From Zero to Hero in Python

Last Updated July 2023

  • 156 lectures
  • All Levels
4.6 (479,964)

Learn Python like a Professional Start from the basics and go all the way to creating your own applications and games | By Jose Portilla

Explore Course

You can also use the contains method of the Python str class directly. Here’s how we would do that:

my_string = ‘This is the string being checked.”
my_substring = ‘string’
if (str.__contains__(string, my_substring)):
    print(‘String contains substring!’)
else:
    print(‘String does not contain substring’)

The first argument is the string you are searching, and the second is the substring you are looking for. It also returns a Boolean. But these methods aren’t the only ones you can use to determine the existence of a substring in a string in Python. You can also use the count, find, and index methods of the Python str class.

Using the Python string index method

The last method to check for a substring we used returned a Boolean value. This will work well in your code if all you need to know is if the substring simply exists in the string. But if you need to know the substring’s location in the string, you will need to use a different method. The index method of the Python string class returns the index of the substring when it finds it.

Here’s an example of where using the index method will be more useful than the contains method. In the following code, we are parsing a greeting that contains the substring {name} and need to know the location of it:

template = ‘Hello {name}, How are you!’
name_index = print(template.index(‘{name}’)) # = 6

One of the issues with using this method is that it works fine if the substring does exist in the string, but otherwise it returns a ValueError.

Using the Python string find method

A better alternative to the index method is the find method. Instead of returning a ValueError when a substring isn’t found, it returns -1. If it finds the substring, it will return the starting index of the first occurrence. Here’s how to use find:

template = ‘Hello {name}, How are you!’
name_index = print(template.find(‘{name}’)) # = 6

The find method is also more flexible than this. What if the string could contain the substring more than once? Here is the syntax of the find method:

string.find(substring, start, end)

We can use the two other arguments to set a starting and ending point in the string we are searching. Here is an example:

my_string = ‘She sells sea shells and sea glass by the sea shore’
my_substring = ‘sea’
print(my_string.find(my_substring, 11, 40)) # = 25

The code above will return the start index of the second occurrence of the substring “sea.”

Using the Python string count method

If all you want to do is count the amount of occurrences of a substring in a string, then it’s best to use the count method of the Python str class. Here is how you would use that:

my_string = ‘She sells sea shells and sea glass by the sea shore’
my_substring = ‘sea’
print(my_string.count(my_substring)) # = 3

Using the Python in operator

This operator is actually just shorthand for the contains method of the Python string class. Using it will call this method to determine if a substring exists in the string in question. You should at least be familiar with this operator because you can also use it to check if an element exists in a tuple, array, or list. Here is how to use the in operator with a string:

my_string = ‘This is the string being checked.”
my_substring = ‘string’
if (my_substring in my_string):
    print(‘String contains substring!’)
else:
    print(‘String does not contain substring’)

Using regular expressions

In just about any programming language, you can use regular expressions. With all the other options available in Python, this is overkill when looking for an exact match. Still, regular expressions can be instrumental if you are looking for a specific pattern of characters. Here is how to use regular expressions to find a substring in a string in Python:

from re import search

my_string = ‘This is the string being checked.”
my_substring = ‘string’
if (search(my_substring, my_string)):
    print(‘String contains substring!’)
else:
    print(‘String does not contain substring’)

Using the operator contains method

At this point, you should have plenty of ways of determining if a string in Python contains a substring. But there’s still one more. It’s similar to the str class contains method and will return a Boolean value, but there is no real advantage to using it. To use it, you must import the operator module.

import operator

my_string = ‘This is the string being checked.”
my_substring = ‘string’
if (operator.contains(string, my_substring)):
    print(‘String contains substring!’)
else:
    print(‘String does not contain substring’)

Things to remember when checking if a Python string contains a substring

All of the methods above will work to find a substring inside of a string. Here are some f the things you need to consider before choosing a method.

To learn even more you can do with Python, make sure to read What is Python Used For, and to learn about an important Python keyword for writing code tests, check out Python Assert.

Page Last Updated: April 2022