How to Check if a Python String Contains a Substring? 5 Methods

Checking to see if a Python string contains a substring is one of the most common things to do in any programming language.

There are different methods that you can use to find out if a string contains a substring in Python.

Why do we need to check if a Python substring contains a string?

Checking to see if a Python string contains a substring can be used for many things, but it is most often used in conditional expressions. In this instance, certain code is executed.

Almost certainly, you’ve encountered the contains function in other programming languages.

Also supported by Python is the __contains__ method.

It also gives several ways that are faster and easier to read for figuring out if a Python string has a substring.

How to check if a string contains a substring python?

A substring is a group of characters that are part of a String.

You can use the following methods to check if the Python string Contains Substring:

  1. Using find() Method
  2. Using in Operator
  3. Using count() Method
  4. Using str.index() Method
  5. Using operator.contains() Method

Method 1: Using find() method

The method find() checks if a string contains a particular substring.

If it does, it returns the starting index of the substring else, it returns -1.

The find() method returns the index of the substring in the string, starting at 0, or -1 if it finds nothing.

Syntax:

string.find(substring)

Example:

Using the find() method to see if a substring is present in a string.

str = "Python for free, offers a free tutorials and projects for all programmers."
substring1 = "Python"
substring2 = "tutorials"

print(str.find(substring1))
print(str.find(substring2))

Output:

0
31

Method 2: Using in operator

The in-operator checks if a substring is present in a string. If it is, it returns true, otherwise, it returns false.

Syntax:

substring in string_object

Example:

Using the in operator to see if a substring is in the string.

str = "Python for free, offers a free tutorials and projects for all programmers."
substring1 = "Python"
substring2 = "tutorials"

print(substring1 in str)
print(substring2 in str)

Output:

True
True

Method 3: Using count() method

The count() method returns the number of times a substring occurs in a string. If the substring is not found, it returns 0.

Syntax:

string.count(substring)

Example:

Using the count() method to see if a substring is present in a string.

str = "Python for free, offers a free tutorials and projects for all programmers."
substring1 = "Python"
substring2 = "tutorials"
substring3 = "Okay"

# this method will count the number of substring inside the string
print(str.count(substring1))
print(str.count(substring2))
print(str.count(substring3))

Output:

1
1
0

Method 4: Using index() method

The index is a string method that checks a string to see if it has a substring. If the substring isn’t in the string, it doesn’t return a value and instead throws a ValueError.

Syntax:

string.index(substring)


Example:

Using the index() method to find out if a substring is in a string.

str = "Python for free, offers a free tutorials and projects for all programmers."
substring1 = "Python"

try:
    result = str.index(substring1)
except ValueError:
    print("Does not exist")
else:
    print(str.index(substring1))

Output:

0

Method 5: Using the operator.contains() method

The operator.contains() method returns the value of boolean True or False, depending on whether or not the specified string object is in the string object.

Keep in mind that Python’s string contains() method is case sensitive.

Syntax:

operator.contains(string,substring)

Example:

Using the operator.contains() method to check if a substring is in the string.

import operator

str = "Python for free, offers a free tutorials and projects for all programmers."

if operator.contains(str, "Python"):
    print("Python is in the string.")
else:
    print("Python is not in the string.")

Output:

Python is in the string.

Summary

This tutorial showed you how to use Python to check if a string contains a substring.

There are a few ways to do this, but the in-operator is the cleanest.

Still, you can become a better programmer if you know how to use the find(), index(), operator.contains(), and count() methods.

You may also check the Python Exit For Loop with Example Program.

Leave a Comment