Skip to content

Python String Contains: Check if a String Contains a Substring

Python String Contains Check if a String Contains a Substring Cover Image

In this tutorial, you’ll learn how to use Python to check if a string contains another substring. There are a number of ways to use Python to check a string for a substring, including case insensitivity, checking for patterns, and more. Being able to work with strings in Python is an important skill for a programmer of any skill level.

By the end of this tutorial, you’ll have learned:

  • How to use Python to check if a string contains a substring, using the in operator, the .index() method, the .find() method and more
  • How to ignore case sensitivity when checking if a Python string contains a substring
  • Filtering a Pandas DataFrame if the column contains a substring
  • Checking if a string contains a pattern substring using regex
  • And more

Checking if a Python String Contains a Substring with in

The simplest and most Pythonic way to check if a string in Python contains a substring is to use the in operator. The operator clearly expresses what you’re trying to accomplish and makes it clear to any reader of your code.

The in operator will return a boolean value: True if the substring is found in the string and False if it isn’t. Let’s take a look at an example:

# Using the in Operator to Check if a String Contains a Substring
a_string = 'Welcome to datagy.io'
print('datagy' in a_string)

# Returns: True

In the example above, we used the in operator to check if the substring 'datagy' exists in the larger string.

Here, it’s important to note that the operator doesn’t check where the substring is, only that it exists. In the following section, you’ll learn how to identify where a substring exists in a larger string.

Checking if a Python String Contains a Substring with find

The Python find string method allows you to return the starting index of a substring, if it exists. This is analogous to many other languages that don’t explicitly support the in operator.

The string.find() method has two possibilities for return values:

  • A positive integer, representing the starting index of the first instance of the substring
  • -1, if the substring doesn’t exist

Let’s take a look at how we can use the .find() method to check if a substring exists in a Python string:

# Using .find() to Check if a String Contains a Substring
a_string = 'Welcome to datagy.io'

if a_string.find('datagy') >= 0:
    print('String exists')
else:
    print("String doesn't exist")

We can see that because the string contains the substring, it returns a positive index value. When a positive index value is returned, the if-else statement prints out that the string exists.

Checking if a Python String Contains a Substring with index

The Python string index method looks for a substring in a broader Python string and returns the beginning index, if the substring exists. This is very similar to the .find() method that we explored in the previous section.

One of the key differences between the Python .find() and .index() methods is that the .index() method will raise a ValueError if the substring is not found.

This means that in order to use this method, we can repeat our earlier example, except we wrap it in a try-except block instead. Let’s see how this works:

# Checking if a String Contains a Substring in Python
a_string = 'Welcome to datagy.io'

try:
    a_string.index('datagy')
    print('String exists')
except ValueError:
    print("String doesn't exist")

Checking if a Python String Contains a Substring with count

The Python string count() method can be used to check if a string contains a substring by counting the number of times the substring exists in the broader string. The method will return the number times the substring exists. This means, that if the substring doesn’t exist, then the method will return 0.

Let’s see how we can use the .count() method to check if a Python string contains a substring:

# Using .count() to Check if a String Contains a Substring
a_string = 'Welcome to datagy.io'

if a_string.count('datagy'):
    print('String exists')
else:
    print("String doesn't exist")

The reason that this works is that if a string doesn’t contain a substring, the count will be 0. In Python, the value 0 evaluates to the boolean False. This means that the else block is triggered.

Filtering a List of Strings if They Contain a Substring

In this section, we’ll use the in operator to filter a list of strings if they contain a substring. In order to do this, we can use a for loop to loop over each item in the list to check if it contains a substring. If it does, then we append it to another list.

Let’s take a look at an example:

# Using a For Loop to Filter a List of Strings
strings = ['hello and welcome', 'to the world of python', 'I am a python programmer', 'just learning']

filtered = []
for string in strings:
    if 'python' in string:
        filtered.append(string)

print(filtered)

# Returns:
# ['to the world of python', 'I am a python programmer']

We can actually simplify this approach significantly by using a list comprehension, as shown below:

# Using a List Comprehension to Filter a List of Strings
strings = ['hello and welcome', 'to the world of python', 'I am a python programmer', 'just learning']

filtered = [string for string in strings if 'python' in string]
print(filtered)

# Returns:
# ['to the world of python', 'I am a python programmer']

Checking if a Python String Contains a Substring without Case Sensitivity

In this section, you’ll learn how to check if a Python string contains a substring without case sensitivity. The simplest way to do this is to check against both strings in their lowered string representations, using the .lower() method.

We can then use the in operator to check if the substring is in the larger string:

# Checking if a Python String Contains a Substring without Case Sensitivity
string = 'Hello and welcome to DataGy'
print('datagy'.lower() in string.lower())

# Returns: True

In the example above, we represent the substring and the broader string in their lower-case representations. This ensures that, regardless of case, the substring can be found if it exists.

Checking if a Python String Contains a Pattern of Text

In this section, you’ll learn how to check if a Python string contains a pattern of text. This can be helpful when you’re trying to see if, for example, a string contains an email address. We can do this by using the powerful regular expression library, re.

Let’s take a look at how we can do this using Python:

# Checking for a Substring in a Python String
import re
string = 'My email address is [email protected]'
pattern = r'[\w\.-]+@[\w\.-]+'

print(bool(re.search(pattern, string)))

# Returns: True

In the example above, we create a pattern that checks for strings, periods and dashes, followed by a @ character, and preceded by the earlier pattern.

By converting the re.search() object to a boolean, the result will return True if the pattern is found and False if it isn’t.

Checking if a Pandas Column Contains a Substring

In this section, you’ll learn how to check if a Pandas column contains a substring. This can be incredibly helpful to filter a Pandas DataFrame based on a column containing a substring.

Pandas makes this easy using the str.contains() method, which checks if a string contains a substring. Let’s take a look at an example:

# Checking if a Pandas Column Contains a Substring
import pandas as pd
df = pd.DataFrame.from_dict({
    'a': [1, 2, 3, 4, 5],
    'b': ['apple, banana', 'orange, banana', 'apple, orange', 'apple, banana', 'orange, banana']
})

print(df[df['b'].str.contains('orange')])

# Returns:
#    a               b
# 1  2  orange, banana
# 2  3   apple, orange
# 4  5  orange, banana

In the example above, we filter the Pandas DataFrame to only include rows where column 'b' contains the substring 'orange'.

Conclusion

In this tutorial, you learned how to use Python to check if a string contains a substring. There are a number of ways to accomplish this, though the most Pythonic and cleanest way is by using the in operator. That said, knowing how to use the .find(), .index(), and .count() methods can make you a stronger programmer.

You also learned how to check if a Python string contains a substring without case sensitivity and be able to check for a pattern in a broader string. Finally, you learned how to filter a Pandas DataFrame if a substring is found in a row.

Additional Resources

To learn more about related topics, check out the tutorials below:

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials.View Author posts

2 thoughts on “Python String Contains: Check if a String Contains a Substring”

  1. I think your example should be:

    if a_string.find(‘datagy’) >= 0:

    instead of:

    if a_string.find(‘datagy’) > 0:

    because currently for a_string.find(“Welcome”) it returns “String doesn’t exist”.

Leave a Reply

Your email address will not be published. Required fields are marked *