Last updated: Apr 10, 2024
Reading timeยท5 min
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.
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')
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
.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
If you need to check if a string doesn't contain a substring in a case-insensitive manner, convert both strings to lowercase.
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')
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.
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.
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')
The str.casefold() method returns a case-folded copy of the string.
# ๐๏ธ 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
.
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.
To check if a string does not contain any strings from a list:
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')
We used a generator expression to iterate over the list.
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).
If you need to check if the string contains at least one of the strings from the
list, use the any()
function instead.
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 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.
any()
function short-circuits and returns True
.If you need to perform a case-insensitive membership test, convert both strings to lowercase.
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 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.
If you need to find the list items that are contained in the string, use a list comprehension.
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']
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.
You can learn more about the related topics by checking out the following tutorials: