Skip to content

Python String Contains: How to Easily Check if a String Contains Another in Python

CodeMDD.io

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

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

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

It’s important to note that in is case-sensitive. That means the substring must have the same capitalization as the original string. If you want to make your string comparison case-insensitive, you can convert both the string and the substring to lowercase or uppercase before performing the in check.

Generalize Your Check by Removing Case Sensitivity

To make your check case-insensitive, you can convert the string and the substring to lowercase or uppercase using the lower() or upper() string methods. Then, you can use the membership operator to check whether the lowercase or uppercase substring is present in the lowercase or uppercase string.

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

Python code:

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 = "SECRET"
lowercase_raw_file_content = raw_file_content.lower()
lowercase_substring = substring.lower()
lowercase_substring in lowercase_raw_file_content

In this code, the lower() method is used to convert both the raw_file_content and substring to lowercase. Then, the membership operator is used to check whether the lowercase substring is present in the lowercase string. This makes the check case-insensitive.

Learn More About the Substring

If you need to find the index position of the first occurrence of a substring inside a string, you can use the find() method. This method returns the index of the substring if it’s found, and -1 if it’s not found.

Here’s an example:

Python code:

quote = "Be kind whenever possible. It is always possible."
index = quote.find("kind")
print(index)

This code will output 3 because the substring “kind” starts at index position 3 in the string. If the substring is not found, the find() method returns -1.

You can also use the index() method to find the index position of a substring. However, unlike find(), if the substring is not found, the index() method raises a ValueError.

Find a Substring With Conditions Using Regex

If you need to find a substring that matches a certain pattern or condition, you can use regular expressions (regex) in Python. The re module provides functions for working with regular expressions.

Here’s an example of how to find a substring that starts with “py” and ends with “thon” using regex:

Python code:

import re
text = "Python is a powerful programming language."
substring = re.findall(r"py\w+thon", text)
print(substring)

The re.findall() function searches for all occurrences of the pattern “py\w+thon” in the text string. This pattern matches any word that starts with “py” and ends with “thon”. The findall() function returns a list of all matching substrings found in the string.

Find a Substring in a pandas DataFrame Column

If you’re working with tabular data in pandas, you can search for substrings in a specific column of a DataFrame using the str.contains() method.

Here’s an example:

Python code:

import pandas as pd
data = {
"Name": ["John Doe", "Jane Smith", "Bob Johnson"],
"Email": ["john.doe@example.com", "jane.smith@example.com", "bob.johnson@example.com"]
}
df = pd.DataFrame(data)
substring = "Doe"
filtered_df = df[df["Name"].str.contains(substring)]
print(filtered_df)

In this code, the str.contains() method is used to check whether the substring “Doe” is present in each value of the “Name” column in the DataFrame. The resulting DataFrame filtered_df contains only the rows where the substring is found in the “Name” column.

Key Takeaways

  • The membership operator in is the recommended way to check whether a string contains a substring in Python.
  • If you want to make your check case-insensitive, you can convert the string and the substring to lowercase or uppercase using the lower() or upper() string methods before using the membership operator.
  • The find() method can be used to find the index position of the first occurrence of a substring in a string.
  • The index() method can also be used to find the index position of a substring, but it raises a ValueError if the substring is not found.
  • Regular expressions can be used to find substrings that match a certain pattern or condition.
  • In pandas, you can use the str.contains() method to search for substrings in a specific column of a DataFrame.

By mastering the techniques covered in this tutorial, you’ll be able to efficiently check whether a string contains a substring and perform different actions based on the result.

CodeMDD.io