Easily Master Python: Essential Tips and Tricks for Beginners
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.
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. The in
membership operator gives you a quick and readable way to check whether a substring is present in a string.
You can use the in
operator to check whether a substring is present in a string by using the following syntax:
If the substring is present in the string, the expression will return True
. Otherwise, it will return False
.
For example:
Output:
In this code snippet, the in
operator is used to check whether the substring “secret” is present in the raw_file_content
string. If it is, then the message “Found!” will be printed to the terminal.
You can also use the not in
operator to check whether a substring is not present in a string. The not in
operator returns True
if the substring is not found, and False
if the substring is found. For example:
Output:
Generalize Your Check by Removing Case Sensitivity
By default, the in
operator performs a case-sensitive search. This means that it will only match substrings that have the same case as the string being searched.
If you want to perform a case-insensitive search, you can convert both the substring and the string being searched to lowercase or uppercase before using the in
operator. This will ensure that the search is not affected by case differences.
Here’s an example:
Output:
In this example, the lower()
method is used to convert both the substring and the string to lowercase before using the in
operator. This allows the code to perform a case-insensitive search and the substring “hello” is found in the string “Hello, World!“.
Learn More About the Substring
If you need to find the index of a substring within a string, you can use the find()
or index()
methods. Both methods will return the starting index of the first occurrence of the substring in the string. If the substring is not found, find()
will return -1, while index()
will raise a ValueError
.
Here’s an example using the find()
method:
Output:
In this example, the find()
method is used to find the index of the substring “llo” in the string “Hello, World!“. The index
variable stores the index of the substring if found, and the code checks if the index is not -1 to determine if the substring is found.
Find a Substring With Conditions Using Regex
If you need more complex search conditions, you can use regular expressions (regex) to find substrings. The re
module in Python provides functions for working with regular expressions.
Here’s an example that uses the re
module to find substrings starting with the letter “a” followed by any two digits:
Output:
In this example, the re.findall()
function is used to find all substrings in the string string
that match the regular expression substring
. The matches
variable stores the list of matching substrings, and the code checks if the list is not empty to determine if any substrings are found.
Find a Substring in a pandas DataFrame Column
To find substrings in pandas DataFrame columns, you can use the str.contains()
method. This method returns a boolean mask that indicates whether each element in the DataFrame column contains the specified substring.
Here’s an example that demonstrates how to use the str.contains()
method to find substrings in a pandas DataFrame column:
Output:
In this example, the str.contains()
method is used to check whether each element in the “Name” column of the pandas DataFrame df
contains the substring “oh”. The matches
variable stores the boolean mask indicating the matches, and the code checks if any matches are found using the any()
method. If matches are found, the code prints the corresponding rows from the DataFrame.
Key Takeaways
In this tutorial, you learned how to check if a Python string contains a substring using the membership operator in
. You also learned how to generalize your check by removing case sensitivity using the lower()
method. Additionally, you learned how to find substrings within a string using find()
or index()
, and how to use regular expressions to find substrings that match specific patterns. Finally, you learned how to find substrings in pandas DataFrame columns using the str.contains()
method.
Now that you know various techniques for checking if a string contains a substring, you can use these methods in your own Python programs to perform different actions based on the presence or absence of certain substrings.