Check if a string does NOT contain substring in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
5 min

banner

# Table of Contents

  1. Check if a string does NOT contain substring in Python
  2. Check if a string does not contain a substring, ignoring the case
  3. Python: Check if String does not contain a Substring using casefold()
  4. Check if String does not contain any Strings from List in Python
  5. Check if a string contains at least one of the strings from a List
  6. Check if a string contains at least one of the strings in a list, ignoring the case
  7. Find the list items that are contained in the string

# Check if a string does NOT contain substring in Python

Use the not in operator to check if a string does not contain a given substring, e.g. if substring not in string:.

The not in operator will return True if the substring is not contained in the string and False otherwise.

main.py
string = 'bobbyhadz.com' substring = 'abc' if substring not in string: # ๐Ÿ‘‡๏ธ this runs print('The string does NOT contain the substring') else: print('The string contains the substring')

check if string does not contain substring

The code for this article is available on GitHub

If you need to check if a string does not contain any strings from a list, click on the following subheading:

The in operator tests for membership. For example, x in s evaluates to True if x is a member of s, otherwise it evaluates to False.

x not in s returns the negation of x in s.
main.py
string = 'bobbyhadz.com' print('bob' in string) # ๐Ÿ‘‰๏ธ True print('bob' not in string) # ๐Ÿ‘‰๏ธ False print('abc' in string) # ๐Ÿ‘‰๏ธ False print('abc' not in string) # ๐Ÿ‘‰๏ธ True

# Check if a string does not contain a substring, ignoring the case

If you need to check if a string doesn't contain a substring in a case-insensitive manner, convert both strings to lowercase.

main.py
string = 'bobbyhadz.com' substring = 'BOB' if substring.lower() not in string.lower(): print('The string does NOT contain the substring') else: # ๐Ÿ‘‡๏ธ this runs print('The string contains the substring')

check if string does not contain substring ignoring case

The code for this article is available on GitHub

The str.lower() method returns a copy of the string with all the cased characters converted to lowercase.

By converting both strings to the same case, we can perform a case-insensitive membership test.

# Python: Check if String does not contain a Substring using casefold()

If your strings may contain non-ASCII characters, use the str.casefold() method instead of str.lower() to check if a string doesn't contain a substring in a case-insensitive manner.

main.py
string = 'bobbyhadz.com' substring = 'BOBBY' if substring.casefold() not in string.casefold(): print('The string does NOT contain the substring') else: # ๐Ÿ‘‡๏ธ this runs print('The string contains the substring')

check if string does not contain substring using casefold

The code for this article is available on GitHub

The str.casefold() method returns a case-folded copy of the string.

main.py
# ๐Ÿ‘‡๏ธ using str.casefold() print('BOBBY'.casefold()) # ๐Ÿ‘‰๏ธ bobby print('รŸ'.casefold()) # ๐Ÿ‘‰๏ธ ss # ๐Ÿ‘‡๏ธ using str.lower() print('BOBBY'.lower()) # ๐Ÿ‘‰๏ธ bobby print('รŸ'.lower()) # ๐Ÿ‘‰๏ธ รŸ

Case folding is similar to lowercasing but is more aggressive because it is intended to remove all case distinctions in a string.

Notice how the German lowercase letter รŸ is equal to ss.

Since the letter is already lowercase, the str.lower() method returns the letter as is, while the str.casefold() method converts it to ss.

Using the str.casefold() method is not necessary if you are only comparing ASCII strings. In this case, using str.lower() is sufficient.

# Check if String does not contain any Strings from List in Python

To check if a string does not contain any strings from a list:

  1. Use a generator expression to iterate over the list.
  2. Check if each list item is not contained in the string.
  3. If the condition is met for all list items, the string doesn't contain any of the strings in the list.
main.py
my_list = ['bobby', 'hadz', 'com'] my_str = 'abc bobby 2468' not_contains = all(item not in my_str for item in my_list) print(not_contains) # ๐Ÿ‘‰๏ธ False if not_contains: print('The string does NOT contain any string from the list') else: # ๐Ÿ‘‡๏ธ this runs print('The string contains at least one of the strings from the list')
The code for this article is available on GitHub

We used a generator expression to iterate over the list.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.
main.py
my_list = ['bobby', 'hadz', 'com'] my_str = 'abc bobby 2468' not_contains = all(item not in my_str for item in my_list) print(not_contains) # ๐Ÿ‘‰๏ธ False

On each iteration, we check if the current list item is not contained in the string and return the result.

The in operator tests for membership. For example, x in s evaluates to True if x is a member of s, otherwise it evaluates to False.

x not in s returns the negation of x in s.

The all() built-in function takes an iterable as an argument and returns True if all elements in the iterable are truthy (or the iterable is empty).

# Check if a string contains at least one of the strings from a List

If you need to check if the string contains at least one of the strings from the list, use the any() function instead.

main.py
my_list = ['bobby', 'hadz', 'com'] my_str = 'abc bobby 2468' contains = any(item in my_str for item in my_list) print(contains) # ๐Ÿ‘‰๏ธ True if contains: print('The string contains at least one of the strings from the list') else: print('The string does NOT contain any of the strings from the list')
The code for this article is available on GitHub

The any() function takes an iterable as an argument and returns True if any element in the iterable is truthy.

On each iteration, we check if the current list item is contained in the string and return the result.

If the condition is met for any of the list items, the any() function short-circuits and returns True.

# Check if a string contains at least one of the strings in a list, ignoring the case

If you need to perform a case-insensitive membership test, convert both strings to lowercase.

main.py
my_list = ['bobby', 'hadz', 'com'] my_str = 'ABC BOBBY 2468' contains = any(item.lower() in my_str.lower() for item in my_list) print(contains) # ๐Ÿ‘‰๏ธ True if contains: print('The string contains at least one of the strings from the list') else: print('The string does NOT contain any of the strings from the list')
The code for this article is available on GitHub

The str.lower() method returns a copy of the string with all the cased characters converted to lowercase.

Convert both strings to lowercase or uppercase allows us to test for membership in a case-insensitive manner.

# Find the list items that are contained in the string

If you need to find the list items that are contained in the string, use a list comprehension.

main.py
my_list = ['bobby', 'hadz', 'com'] my_str = 'abc bobby 2468' matches = [item for item in my_list if item in my_str] print(matches) # ๐Ÿ‘‰๏ธ ['bobby']
The code for this article is available on GitHub

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

The new list contains only the strings that are contained in the other string.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev