Skip to main content

How to Check String Containment in Python

This guide explains how to check if a string contains a substring in Python, and covers both case-sensitive and case-insensitive checks. We'll also cover how to check if a string contains any or all of a list of substrings, and how to find which substrings from a list are present in a string.

Checking for Substring Presence with in and not in

The most direct and Pythonic way to check if a string contains a substring is to use the in operator:

string = 'tutorialreference.com'
substring = 'reference'

if substring in string:
print('The string contains the substring')
else:
print('The string does NOT contain the substring')

Output:

The string contains the substring

To check if a substring is not present, use not in:

string = 'tutorialreference.com'
substring = 'abc'

if substring not in string:
print('The string does NOT contain the substring') # Output: The string does NOT contain the substring
else:
print('The string contains the substring')

print('tut' in string) # Output: True
print('tut' not in string) # Output: False
print('abc' in string) # Output: False
print('abc' not in string) # Output: True
  • in and not in are very efficient for substring checks. They are almost always the best choice for simple presence/absence checks.

Case-Insensitive Substring Checks

By default, in is case-sensitive. For case-insensitive checks, convert both the string and the substring to lowercase (or uppercase) before using in.

Using str.lower()

string = 'tutorialreference.com'
substring = 'TUT'

if substring.lower() in string.lower():
print('The string contains the substring (case-insensitive)') # Output: The string contains the substring
else:
print("not found")

note

string.lower() and substring.lower() convert both strings to lowercase before the comparison.

Using str.casefold() (for more comprehensive case-folding)

For more robust case-insensitive comparisons, especially when dealing with non-English characters, use str.casefold():

string = 'tutorialreference.com'
substring = 'TUTORIAL'

if substring.casefold() in string.casefold():
print('The string contains the substring (case-insensitive)')
# Output: The string contains the substring (case-insensitive)
else:
print("not found")
note

casefold() is more aggressive than lower() and handles more Unicode cases correctly. It converts strings to a caseless form suitable for comparisons.

Checking if a String Contains ANY Substring from a List

To efficiently check if a string contains any of a list of substrings, use the any() function with a generator expression:

my_list = ['tutorial', 'reference', 'com']
my_str = 'abc tutorial 1234'

contains = any(item in my_str for item in my_list)
print(contains) # Output: True

if contains:
print('The string contains at least one of the substrings')
else:
print('The string does NOT contain any of the substrings')
note

any(item in my_str for item in my_list): This generator expression efficiently checks each item from my_list to see if it's a substring of my_str. any() short-circuits (stops and returns True) as soon as it finds a match.

If you want to ignore case:

my_list = ['tutorial', 'reference', 'com']
my_str = 'ABC TUTORIAL 1234'

contains = any(item.lower() in my_str.lower() for item in my_list)
print(contains) # Output: True

Checking if a String Contains ALL Substrings from a List

To check if a string contains all substrings from a list, use all() with a generator expression:

my_list = ['tutorial', 'reference', 'com']
my_str = 'abc tutorial 1234'

not_contains = all(item not in my_str for item in my_list)
print(not_contains) # Output: False

if not_contains:
print('The string does NOT contain any string from the list')
else:
print('The string contains at least one of the strings from the list')
# Output: The string contains at least one of the strings from the list
note

all(item not in my_str for item in my_list): This generator expression checks if none of the item from my_list is a substring of my_str. all() will return True only if all the substrings are not found.

Finding Matching Substrings from a List

If you want to extract the substrings from the list that are present in the string, use a list comprehension:

my_list = ['tutorial', 'reference', 'com']
my_str = 'abc tutorial 1234'

matches = [item for item in my_list if item in my_str]
print(matches) # Output: ['tutorial']
note

The list comprehension iterates over the elements and uses the in method to check if a given element is present in the string, returning all elements that are present.