Skip to content

How to Check if a String Contains a Specific Word in Python

[

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

In some cases, you may want to check for the existence of a substring in a case-insensitive manner. For example, you might want to determine whether a string contains “Python” regardless of whether it’s capitalized or in all lowercase. To achieve this, you can convert both the string and the substring to a specific case before performing the membership check.

Here’s an example:

text = "This is a string with Python in it"
substring = "python"
if substring.lower() in text.lower():
print("Found!")

In this code snippet, you use the lower() string method to convert both text and substring to lowercase before performing the membership check. This ensures that the check is case-insensitive and will find the substring regardless of the capitalization.

Learn More About the Substring

Sometimes, it’s not enough to know whether a substring exists in a string. You may also want to know the index or position of the substring in the string, or how many times it occurs. Python provides several string methods that can help you gather more information about your substring.

Here are a few examples:

  • To find the first occurrence of a substring, you can use the find() method:
text = "This is a string with Python in it"
substring = "Python"
index = text.find(substring)
print(index)

The output will be the index (position) of the first occurrence of the substring in the string. If the substring is not found, the method will return -1.

  • To count the number of occurrences of a substring in a string, you can use the count() method:
text = "This is a string with Python in it"
substring = "i"
count = text.count(substring)
print(count)

The output will be the number of times the substring appears in the string.

These are just a few examples of the string methods you can use to gather more information about your substring. Python provides many more useful methods that you can explore to suit your specific needs.

Find a Substring With Conditions Using Regex

If you need to find a substring in a string with specific conditions, you can use regular expressions (regex). Regex allows you to define patterns and search for substrings that match those patterns. This gives you powerful flexibility in searching for complex substrings.

Here’s an example:

import re
text = "This is a string with some numbers: 12345"
pattern = r"\d+"
matches = re.findall(pattern, text)
print(matches)

In this code snippet, you import the re module and define a regex pattern that matches one or more digits (\d+). You then use the findall() function from the re module to find all occurrences of substrings that match the pattern in the text. The output will be a list of all the matches found.

Regex provides a wide range of patterns and functionalities that you can use to search for specific substrings. It’s a powerful tool that can be extremely useful in complex string searching scenarios.

Find a Substring in a pandas DataFrame Column

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

Here’s an example:

import pandas as pd
df = pd.DataFrame({
"Name": ["Alice", "Bob", "Charlie", "David"],
"Age": [25, 30, 35, 40],
"Job": ["Software Engineer", "Data Analyst", "Product Manager", "Marketing Specialist"]
})
substring = "Engineer"
filtered_df = df[df["Job"].str.contains(substring)]
print(filtered_df)

In this code snippet, you create a DataFrame with columns for Name, Age, and Job. You then use the .str.contains() method to filter the DataFrame based on whether the “Job” column contains the substring “Engineer”. The resulting DataFrame will only contain the rows where the substring is found.

This method is particularly useful when working with large datasets and filtering rows based on the presence of specific substrings.

Key Takeaways

  • You can use the membership operator in to check whether a string contains a substring in Python.
  • The in operator returns a Boolean value: True if the substring is found, and False if it’s not found.
  • Python provides several string methods, such as find() and count(), that you can use to gather more information about substrings.
  • Regular expressions (regex) provide a powerful way to search for substrings with specific conditions.
  • In pandas, you can use the .str.contains() method to find substrings in a specific column of a DataFrame.

By mastering these techniques, you’ll be able to efficiently search for substrings in Python and perform custom actions based on their presence or absence.