How to check if a substring is contained in another string in Python?


A substring is a contiguous sequence of characters within a string. In other words, it's a smaller piece of text that appears within a larger piece of text.

For example, in the string "lorem ipsum", the substring "lorem" appears within the larger string. Similarly, the substring "em i" appears within the larger string "lorem ipsum" as well.

Substring operations are common in programming and can be used for various tasks such as searching for a specific word in a text, extracting parts of a text, or replacing certain parts of a text with other words.

Checking if a substring is contained within a string in Python is a common task, and can be done using several different methods. Let us consider some of the most popular methods below, along with code examples that you can try out yourself.

Using the ‘in’ operator

The simplest way to check if a substring is contained within a string is to use the in operator. Here's how you can do it:

Example

In this code, we define a string variable called string that contains the text "lorem ipsum". We also define a substring variable called substring that contains the text "ipsum".

We then use an if statement to check if the substring is contained within the string. The in operator checks if substring is a substring of string, and returns True if it is, or False if it is not.

If the substring is found within the string, we print the message "Substring found!". Otherwise, we print the message "Substring not found.".

string = "lorem ipsum"

substring = "ipsum"
if substring in string:
    print("Substring found!")
else:
    print("Substring not found.")

Output

Substring found!

Using the find() method

Another way to check if a substring is contained within a string is to use the find() method. Here's an example:

Example

In this code, we define the string and substring variables as shown.

We then use the find() method of the string variable to check if the substring is contained within the string. The find() method searches for the substring within the string, and returns the index of the first occurrence of the substring, or -1 if the substring is not found.

We use an if statement to check if the result of string.find(substring) is not equal to -1. If it is not equal to -1, then the substring is found within the string, so we print the message "Substring found!". Otherwise, we print the message "Substring not found.".

string = "lorem ipsum"
substring = "dolor"
if string.find(substring) != -1:
    print("Substring found!")
else:
    print("Substring not found.")

Output

Substring not found.

Using regular expressions

Another way to check if a substring is contained within a string is to use regular expressions.

Example

In this code, we first import the re module, which provides support for regular expressions in Python.

We then define the string and substring variables as before.

We use the re.search() function to search for the substring within the string. The re.search() function searches the string for a match to the regular expression represented by the substring.

If a match is found, the re.search() function returns a match object, which is considered to be True in a Boolean context. If no match is found, the function returns None, which is considered to be False in a Boolean context.

We use an if statement to check if the result of re.search(substring, string) is True. If it is, then the substring is found within the string, so we print the message "Substring found!". Otherwise, we print the message "Substring not found.".

import re
string = "lorem ipsum"
substring = "ipsum"

if re.search(substring, string):
    print("Substring found!")
else:
    print("Substring not found.")

Output

Substring found!

Using the startswith() method

One different way to check if a substring is contained within a string is to use the startswith() method.

Example

In this code, we define the string and substring variables as before.

We use the startswith() method of the string variable to check if the substring is at the beginning of the string. The startswith() method returns True if the string starts with the substring, and False otherwise.

We use an if statement to check if the result of string.startswith(substring) is True. If it is, then the substring is found within the string, so we print the message "Substring found!". Otherwise, we print the message "Substring not found.".

string = "lorem ipsum"
substring = "dolor"

if string.startswith(substring):
    print("Substring found!")
else:
    print("Substring not found.")

Output

Substring not found.

Using list comprehension

One more way to check if a substring is contained within a string is to use list comprehension.

Example

In this code, we again define the string and substring variables as before.

We use a list comprehension to split the string into individual words and check if the substring is contained within any of those words. The split() method of the string variable splits the string into a list of words, using whitespace as the delimiter. The list comprehension then checks if the substring is contained within each of the words in the list.

We use the any() function to check if any of the values in the list returned by the list comprehension are True. The any() function returns True if at least one of the values in the list is True, and False otherwise.

We use an if statement to check if the result of any([substring in word for word in string.split()]) is True. If it is, then the substring is found within the string, so we print the message "Substring found!". Otherwise, we print the message "Substring not found.".

string = "lorem ipsum"
substring = "lorem"
if any([substring in word for word in string.split()]):
    print("Substring found!")
else:
    print("Substring not found.")

Output

Substring found!

So those are some different ways to check if a substring is contained within a string in Python! Depending on your specific use case, one method may be more appropriate than another. But in general, the ‘in’ operator and the find() method are the most commonly used methods for this task.

Updated on: 10-Aug-2023

193 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements