Skip to content

How to Check String Containment in Python

Learn how to check if a Python string contains a substring (case-sensitive or case-insensitive) and efficiently verify presence of any/all substrings from a list.

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")

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)')
else:
    print("not found")

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')

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')

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']
Navigation