Skip to content

Check if String Contains Substring

[

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:

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:

"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:

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 in operator is case-sensitive, meaning that it will only match substrings with the same casing. If you need to check for a substring regardless of casing, you can convert both the string and substring to either uppercase or lowercase before running the check.

Here’s an example of how to make your check case-insensitive:

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

In this case, the lower() method is used to convert both the string and the substring to lowercase before checking for membership. This ensures that the check is no longer case-sensitive.

Learn More About the Substring

In addition to simply checking for the existence of a substring, you may also want to extract the substring from the string or find its position within the string. Python provides various methods to accomplish these tasks.

Extracting the Substring

To extract a substring from a string, you can use slicing. Slicing allows you to specify a range of indices to extract a portion of the string.

Here’s an example that extracts the substring “hidden”:

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."""
substring = raw_file_content[28:34]
print(substring)

The output of this code will be “hidden”.

Finding the Position of the Substring

If you want to find the position of a substring within a string, you can use the index() method. This method returns the starting index of the first occurrence of the substring in the string.

Here’s an example:

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."""
position = raw_file_content.index("hidden")
print(position)

The output of this code will be 28, indicating that the substring “hidden” starts at index position 28 in the string.

Find a Substring With Conditions Using Regex

If you need to find a substring that matches a specific pattern, you can use regex (regular expressions) in Python. Regex allows you to define complex patterns to search for within a string.

Here’s an example that uses regex to find all occurrences of the substring “secret” followed by any four characters:

import re
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."""
pattern = r"secret...."
matches = re.findall(pattern, raw_file_content)
print(matches)

The output of this code will be a list of matches: ['secret sec']. The pattern "secret...." matches the substring “secret” followed by any four characters.

Find a Substring in a pandas DataFrame Column

If you’re working with tabular data and need to find substrings in a pandas DataFrame column, there are different approaches you can take. One option is to use the str.contains() method.

Here’s an example that searches for rows in a DataFrame where the “name” column contains the substring “John”:

import pandas as pd
data = {
"name": ["John Doe", "Jane Smith", "Sam Johnson"],
"age": [25, 30, 35]
}
df = pd.DataFrame(data)
filtered_df = df[df["name"].str.contains("John")]
print(filtered_df)

The output of this code will be:

name age
0 John Doe 25

The ["name"].str.contains("John") expression filters the DataFrame to only include rows where the “name” column contains the substring “John”.

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 and how to extract substrings and find their positions within a string. Additionally, you discovered how to find substrings using regex and how to search for substrings in a pandas DataFrame column. These skills will help you effectively work with and manipulate strings in Python.