Summary: In this post, we will learn multiple ways to check if a string contains another substring in Python.

Example:

String    :  "Let's code in Python"
Substring :  "Python"
Output    :  True

String    :  "Pencil Programmer"
Substring :  "Pen"
Output    :  False

There are multiple ways we can check if a string contains a substring in Python. Let’s discuss each method with examples.

Method 1: Using in Operator

The in operator in Python returns True if the specified iterable object contains another object (in this case is a string).

The following example uses the in operator to check if a string contains another string in it or not:

string = "Today is coding day"
substring = "coding"
 
if substring in string:
    print("Found")
else:
    print("Not Found")

Output: Found

The in operator is similar to __contains__ method, it can also be used to check for items in data structures such as list, tuple, etc.

Method 2: Using find() method

Alternatively, we can use find() method check for a substring in Python.

If the given substring is present in the main string object, it returns the index position of the first letter of the substring, otherwise returns -1.

The general syntax for the find method is:

string.find(<substring>)

Here is a Python example that uses the find() method to check for substring in another string:

string = "Pencil Programmer"
substring = "Pencil"

if string.find(substring) != -1:
    print("Found")
else:
    print("Not Found")

Output: Found

Method 3: Using index() method

index() is another Python string class method that can be used to find the index of the first occurrence of a substring in the given string.

It is a little different from the find() method. If the substring is not present in the string then it throws a ValueError, which needs to be handled using Python try and except.

The general syntax for the index method is:

string.index(<substring>)

Here is a Python example that uses the index() method:

string = "I love Programming"
substring = "love"

try:
    string.index(substring)
except ValueError:
    print("Not Found")
else:
    print("Found")

Output: Found

Tip: If you don’t want to handle any exception then use find() method instead of index().

Method 4: Using count() method

count() is another method in python of type string that returns the count of a substring in the string i.e. number of occurrences of a substring in the string.

It returns 0 if the substring is not found.

The syntax for the count() method is:

string.count(<substring>)

Python example using the count() method:

string = "This is a programming website"
substring = "website"

if string.count(substring) > 0:
    print("Found")
else:
    print("Not Found")

Output: Found

Tip: count() method is useful for knowing the number of occurrences of the substring in the parent string.

Method 5: Using REGEX

REGEX (Regular Expression) provides a more flexible way to search for patterns or substrings in the string.

In REGEX, We first define the pattern or substring as a string, and then we search the pattern in the parent string using search() method.

The search() method comes as a part of a python built-in module called re. So, in order to use this method, we have to first import the re module.

The syntax for the search() method is

search(string,<substring>)
from re import search

string = "PencilProgrammer.com"
substring = ".com"

if search(substring, string):
    print("Found")
else:
    print("Not found")

Output: Found

It is recommended to use REGEX when we intend to do complex operations on strings, as it is slower than all the methods which we have discussed in this post.

In this tutorial, we learned multiple ways to check if a string in python contains another string as a substring.

Leave a Reply