- Python str contains any
- # Table of Contents
- # Check if a string does NOT contain substring in Python
- # Check if a string does not contain a substring, ignoring the case
- # Python: Check if String does not contain a Substring using casefold()
- # Check if String does not contain any Strings from List in Python
- # Check if a string contains at least one of the strings from a List
- # Check if a string contains at least one of the strings in a list, ignoring the case
- # Find the list items that are contained in the string
- # Additional Resources
- Python check if string contains another string
- Python check if string contains another string
- Using find() to check if a string contains another substring
Python str contains any
Last updated: Feb 22, 2023
Reading time · 5 min
# Table of Contents
# 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.
Copied!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 .
Copied!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.
Copied!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.
# 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.
Copied!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.
Copied!# 👇️ 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:
- Use a generator expression to iterate over the list.
- Check if each list item is not contained in the string.
- If the condition is met for all list items, the string doesn’t contain any of the strings in the list.
Copied!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.
Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.
Copied!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 .
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.
Copied!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.
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.
Copied!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.
# 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.
Copied!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.
# 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.
Python check if string contains another string
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
String manipulation is a common task in any programming language. Python provides two common ways to check if a string contains another string.
Python check if string contains another string
Python string supports in operator. So we can use it to check if a string is part of another string or not. The in operator syntax is:
It returns True if “sub” string is part of “str”, otherwise it returns False . Let’s look at some examples of using in operator in Python.
str1 = 'I love Python Programming' str2 = 'Python' str3 = 'Java' print(f'"" contains "" = ') print(f'"" contains "" = ') print(f'"" contains "" = ') if str2 in str1: print(f'"" contains ""') else: print(f'"" does not contain ""')
"I love Python Programming" contains "Python" = True "I love Python Programming" contains "python" = False "I love Python Programming" contains "Java" = False "I love Python Programming" contains "Python"
If you are not familiar with f-prefixed strings in Python, it’s a new way for string formatting introduced in Python 3.6. You can read more about it at f-strings in Python. When we use in operator, internally it calls __contains__() function. We can use this function directly too, however it’s recommended to use in operator for readability purposes.
s = 'abc' print('s contains a =', s.__contains__('a')) print('s contains A =', s.__contains__('A')) print('s contains X =', s.__contains__('X'))
s contains a = True s contains A = False s contains X = False
Using find() to check if a string contains another substring
We can also use string find() function to check if string contains a substring or not. This function returns the first index position where substring is found, else returns -1.
str1 = 'I love Python Programming' str2 = 'Python' str3 = 'Java' index = str1.find(str2) if index != -1: print(f'"" contains ""') else: print(f'"" does not contain ""') index = str1.find(str3) if index != -1: print(f'"" contains ""') else: print(f'"" does not contain ""')
"I love Python Programming" contains "Python" "I love Python Programming" does not contain "Java"
You can checkout complete python script and more Python examples from our GitHub Repository.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases. Learn more about us