Skip to content

Effortlessly Use Python to Solve Complex Problems

[

How to Check if a Python String Contains a Substring

If you’re new to programming or come from a programming language other than Python, you may be looking for the best way to check whether a string contains another string in Python.

Identifying such substrings comes in handy when you’re working with text content from a file or after you’ve received user input. You may want to perform different actions in your program depending on whether a substring is present or not.

In this tutorial, you’ll focus on the most Pythonic way to tackle this task, using the membership operator in. Additionally, you’ll learn how to identify the right string methods for related, but different, use cases.

Finally, you’ll also learn how to find substrings in pandas columns. This is helpful if you need to search through data from a CSV file. You could use the approach that you’ll learn in the next section, but if you’re working with tabular data, it’s best to load the data into a pandas DataFrame and search for substrings in pandas.

How to Confirm That a Python String Contains Another String

If you need to check whether a string contains a substring, use Python’s membership operator in. In Python, this is the recommended way to confirm the existence of a substring in a string:

Python

raw_file_content = """Hi there and welcome.
This is a special hidden file with a SECRET secret.
I don't want to tell you The Secret,
but I do want to secretly tell you that I have one."""
"secret" in raw_file_content

The in membership operator gives you a quick and readable way to check whether a substring is present in a string. You may notice that the line of code almost reads like English.

Note: If you want to check whether the substring is not in the string, then you can use not in:

Python

"secret" not in raw_file_content

Because the substring “secret” is present in raw_file_content, the not in operator returns False.

When you use in, the expression returns a Boolean value:

  • True if Python found the substring
  • False if Python didn’t find the substring

You can use this intuitive syntax in conditional statements to make decisions in your code:

Python

if "secret" in raw_file_content:
print("Found!")

In this code snippet, you use the membership operator to check whether “secret” is a substring of raw_file_content. If it is, then you’ll print a message to the terminal.

Generalize Your Check by Removing Case Sensitivity

By default, the membership operator performs a case-sensitive check. This means that it’s looking for an exact match of the characters in the substring.

If you want to perform a case-insensitive check, you can convert both the string and the substring to lowercase using the lower() method:

Python

string = "Hello World"
substring = "WORLD"
if substring.lower() in string.lower():
print("Match found!")

The lower() method returns a lowercase version of the string, allowing you to perform a case-insensitive check. In this example, even though the case of the substring doesn’t match the case of the string, the code still prints “Match found!“.

Learn More About the Substring

In addition to confirming the presence of a substring, you may also need to know additional details about the substring, such as its position in the string.

To find the index of the first occurrence of a substring within a string, you can use the find() method:

Python

string = "Hello World"
substring = "orl"
index = string.find(substring)
print(index)

The find() method returns the index of the first occurrence of the substring in the string. In this example, the code prints “7” because “orl” is found starting at index 7 in the string “Hello World”.

If the substring is not found, the find() method returns -1.

You can also find the index of the last occurrence of a substring using the rfind() method:

Python

string = "Hello World"
substring = "o"
index = string.rfind(substring)
print(index)

The rfind() method searches the string from right to left and returns the index of the last occurrence of the substring. In this example, the code prints “7” because the last occurrence of “o” is found at index 7 in the string “Hello World”.

If the substring is not found, the rfind() method returns -1.

Find a Substring With Conditions Using Regex

If you need to find a substring that matches certain conditions, such as a specific pattern of characters, you can use regular expressions, or regex.

Python provides the re module for working with regular expressions. Here’s an example of how to find a substring using regex:

Python

import re
string = "Hello 123 World! 456"
pattern = r"\d+"
matches = re.findall(pattern, string)
print(matches)

In this example, the regular expression pattern r"\d+" matches one or more digits. The findall() function from the re module is used to find all the substrings in the string that match the pattern. The code prints ["123", "456"] because those are the substrings that match the pattern.

Regex provides powerful and flexible ways to search for substrings based on specific patterns or conditions. If you need to perform complex substring searches, exploring regex is recommended.

Find a Substring in a pandas DataFrame Column

If you’re working with tabular data and need to search for substrings in a particular column of a pandas DataFrame, you can use the str.contains() method.

Here’s an example of how to find all the rows in a DataFrame that contain a certain substring:

Python

import pandas as pd
data = {"Name": ["John Doe", "Jane Smith", "Bob Johnson"],
"Age": [25, 30, 35]}
df = pd.DataFrame(data)
substring = "ohn"
filtered_df = df[df["Name"].str.contains(substring)]
print(filtered_df)

In this example, a DataFrame is created with a “Name” column and an “Age” column. The str.contains() method is used to check whether each value in the “Name” column contains the substring “ohn”. The code prints the rows in the DataFrame where the substring was found.

Using str.contains() allows you to filter rows based on substring matches in a specific column of a pandas DataFrame.

Key Takeaways

In this tutorial, you learned how to use the membership operator in to check whether a Python string contains a substring. You also learned how to generalize your check by removing case sensitivity, find additional details about a substring using the find() and rfind() methods, find substrings with conditions using regex, and find substrings in pandas DataFrame columns using the str.contains() method.

These techniques allow you to efficiently work with strings and perform tasks such as searching, filtering, and decision-making in your Python programs.