Skip to content

How to Effortlessly Use python str.contains

[

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

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 membership operator is case-sensitive. This means that if you’re searching for the substring "secret" and the string contains a capitalized version of it, such as "Secret", then the operator will return False.

However, Python provides you with a way to remove case sensitivity from your check. The idea is to convert both the string and the substring to the same case before performing the check.

Here’s an example of how to do that:

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.lower()

In this updated code snippet, you convert raw_file_content to lowercase using the lower() string method. Then, you use the in operator to check whether "secret" is a substring of the lowercase version of raw_file_content.

By doing this, you can make your check case-insensitive. Now, even if the string contains a capitalized version of the substring, the operator will return True:

"secret" not in raw_file_content.lower()

Note that the use of in with lower() is most effective when you’re comparing strings in a case-insensitive manner.

Learn More About the Substring

Now that you know how to check whether a string contains a substring, you might want to learn more about the substring itself. Python provides several useful string methods that can help you manipulate and extract information from substrings.

One such method is startswith(). This method checks whether a string starts with a specified substring. For example:

substring = "Hello"
string.startswith(substring)

Similarly, you can use the endswith() method to check whether a string ends with a specified substring. For example:

substring = "Python!"
string.endswith(substring)

Another useful method is find(). This method returns the index of the first occurrence of a specified substring in a string, or -1 if the substring is not found. For example:

substring = "Python"
string.find(substring)

You can also use the replace() method to replace all occurrences of a substring with another string. For example:

substring = "Python"
replacement = "World"
string.replace(substring, replacement)

The output of this code snippet is "Hello, Real World!", since all occurrences of the substring "Python" have been replaced with the string "World".

These are just a few examples of the many string methods available in Python. By exploring and experimenting with these methods, you can gain a deeper understanding of how to manipulate and extract information from substrings.

Find a Substring With Conditions Using Regex

So far, you’ve learned how to check whether a string contains a substring using the in membership operator. However, what if you need to find a substring that matches a specific condition?

In these cases, you can use regular expressions, or regex for short. Regex allows you to define patterns that match specific strings. Python provides the re module for working with regex.

Here’s an example of how to find a substring using regex:

import re
pattern = r"[A-Z][a-z]+"
matches = re.findall(pattern, string)

In this code snippet, you import the re module and define a pattern using regex. The pattern [A-Z][a-z]+ matches any capitalized word in the string.

Next, you use the findall() function from the re module to find all substrings that match the pattern in the string. The output of this code snippet is a list containing the matches ['Hello', 'Real', 'Python'].

Regex provides powerful pattern matching capabilities, allowing you to find substrings that adhere to complex conditions. With regex, you can specify patterns that match specific characters, words, or even complex structures.

Find a Substring in a pandas DataFrame Column

If you’re working with tabular data, such as data from a CSV file, you can use pandas to search for substrings in specific columns of a DataFrame.

Here’s an example of how to find a substring in a pandas DataFrame column:

import pandas as pd
data = {
'name': ['Alice', 'Bob', 'Charlie'],
'age': [25, 30, 35]
}
df = pd.DataFrame(data)
substring = 'ob'
df[df['name'].str.contains(substring)]

In this code snippet, you import the pandas module and create a DataFrame with two columns: ‘name’ and ‘age’.

Next, you define a substring that you want to search for in the ‘name’ column.

Finally, you use the str.contains() method on the ‘name’ column of the DataFrame to create a boolean mask, which you can use to filter the DataFrame and only keep the rows that contain the substring. The output of this code snippet is a DataFrame that contains the rows where the ‘name’ column contains the substring ‘ob’.

By using pandas, you can easily search for substrings in specific columns of tabular data, allowing you to perform more complex analyses on your data.

Key Takeaways

In this tutorial, you learned how to check whether a Python string contains a substring. The membership operator in is the recommended way to perform this check, as it provides a quick and readable syntax.

You also learned how to generalize your check by removing case sensitivity, allowing you to perform case-insensitive searches for substrings.

Additionally, you explored various string methods that can help you manipulate and extract information from substrings. These methods, such as startswith(), endswith(), find(), and replace(), provide powerful tools for working with substrings in Python.

Finally, you discovered how to find substrings that match specific conditions using regular expressions. Regex allows you to define complex patterns that can match specific strings.

If you’re working with tabular data, you can use pandas to search for substrings in specific columns of a DataFrame, enabling more complex analyses of your data.

With these techniques, you’ll be well-equipped to tackle substring-related tasks in Python.