How to Check If a String Contains a Substring in Python

Check if a string contains a substring in Python using the in operator, which is a fast way to check if a substring is contained in another string.

Checkout this video:

Introduction

Python provides various operators to check if a string contains a substring. These operators are--> in, not in, +, %, and ?.

Operator Description
in Returns True if a string contains a substring
not in Returns True if a string does not contain a substring
+ Concatenates two strings
% Prints a formatted string
? Prints the character at the given index

What is a substring?

In Python, a substring is a sequence of characters within a string. For example, the string “I am learning Python” has the substring “learning” within it. Checking if a string contains a substring is a common operation in programming.

There are multiple ways to check if a string contains a substring in Python. The most common way is to use the in operator. The in operator returns True if the specified substring is present in the given string, and False otherwise.

For example, let’s check if the string “I am learning Python” contains the substring “Python”:

string = “I am learning Python”
substring = “Python”

if substring in string:
print(“The string ‘{}’ contains the substring ‘{}’.”.format(string, substring))
else:
print(“The string ‘{}’ does not contain the substring ‘{}’.”.format(string, substring))

Another way to check if a string contains a substring is to use the find() method. The find() method returns the index of the first occurrence of the specified substring, or -1 if the specified substring is not present in the given string.

For example, let’s check if the string “I am learning Python” contains the substring “Python”:

string = “I am learning Python”
substring = “Python” # Index of first occurrence of substr # Index of last occurrence of substr (9) # Index of ‘n’ from last (13) # Index of ‘n’ from start (2)
if string.find(substring) != -1: # If substr exists in str … print(“The string ‘{}’ contains ‘{}’.”.format(string,substring)) else: # Otherwise … print(“The string ‘{}’ does not contain ‘{}’.”.format(string,substring))

Why would you want to check if a string contains a substring?

There are many reasons you might want to check if a string contains a substring. For example, you might want to check if a user’s input contains profanity, or if a file path contains the correct file extension.

In Python, there are several ways to check if a string contains a substring. The most common way is to use the in operator. This approach is fast and easy to read.

Another way to check if a string contains a substring is to use the find() method. This method returns the index of the first occurrence of the substring, or -1 if the substring is not found.

You can also use the index() method, which is similar to find(). However, index() will raise an error if the substring is not found, while find() will return -1.

Finally, you can use regular expressions to check if a string contains a substring. This approach is more powerful but also more complex.

How to check if a string contains a substring in Python

In Python, the easiest way to check if a string contains a substring is to use the in keyword.

Here is an example:

“`
my_string = “Hello, world!”

if “Hello” in my_string:
print(“Found Hello”)
else:
print(“Did not find Hello”)

my_string = “Hello, world!” # Hello is at index 0 # World is at index 7

print(my_string.find(“Hello”)) # prints 0

print(my_string.find(“World”)) # prints 7

“`

Using the in operator

In Python, the in operator is used to check if a string contains a substring. The in operator is used to check if a string contains a substring. The syntax for the in operator is:

substring in string
The in operator returns True if the substring is found in the string. The in operator returns False if the substring is not found in the string. For example,

>>> “Hello” in “Hello World”
True

Using the find() method

The find() method takes two parameters: the substring to be found, and an optional start position. The find() method returns the lowest index in the string where substring is found. It returns -1 if substring is not found.

If start is not specified, it defaults to 0. Note that find() evaluates from left to right, so if the substring is found more than once, only the first occurrence will be returned:

>>> s = “hello world”
>>> s.find(“l”)
2
>>> s.find(“l”, 3)
3

Using the index() method

To check if a string contains a substring, you can use the Python in operator. This method takes two parameters:

The first parameter is the substring you want to find.
The second parameter is the string you want to search.
If the specified substring is found, it returns the index of the first character of the substring. If not found, it returns -1.

For example, let’s take a look at how to use this method to check if a string contains a substring “cat”. We’ll also check if it contains “dog”:

Using the count() method

The count() method returns the number of times a substring occurs in a string. Note that this method is case-sensitive, so it will not find the substring “python” if it is capitalized (i.e., “Python”).

To use the count() method, simply pass in the substring you want to find as an argument. For example, to see how many times “s” appears in “Mississippi”, we would do the following:

Mississippi = “Mississippi”
print(Mississippi.count(“s”))

This would return a value of 2, indicating that the substring “s” appears twice in the string “Mississippi”.

Conclusion

In this article, we’ve gone over three ways to check if a string contains a substring in Python. We started with the simplest method, using the in keyword. Then, we looked at the performance of two other methods: the find method and regular expressions.

Of the three methods, regular expressions were the fastest, followed by the in keyword and then the find method. However, all three methods were fast enough for most applications. If you’re concerned about performance, you should use regular expressions. Otherwise, you can use whichever method is easiest for you to understand and maintain.

Further Reading

If you want to learn more about checking if a string contains a substring in Python, you can read the following article:

https://www.digitalocean.com/community/tutorials/how-to-check-if-a-string-contains-a-substring-in-python

Scroll to Top