Check a string for specific characters in Python

avatar

Borislav Hadzhiev

Last updated: Sep 19, 2022

banner

Photo from Unsplash

Check a string for specific characters in Python #

Use the in operator to check a string for specific characters. You can use the boolean and operator to check if a string contains all of the specified characters or the boolean or operator to check if the string contains at least one of the characters.

main.py
my_str = 'bobby_hadz.com@' # ✅ check if all of the specified characters are contained in the string if ('_' in my_str) and ('.' in my_str) and ('@' in my_str): # 👇️ this runs print('The string contains the specified characters') else: print('The string does NOT contain the specified characters') print('_' in my_str) # 👉️ True print('!' not in my_str) # 👉️ True # --------------------------------------------- # ✅ check if at least one the specified characters is contained in the string my_str = 'bobbyhadz.com' if ('_' in my_str) or ('.' in my_str) or ('@' in my_str): # 👇️ this runs print('The string contains at least one of the specified characters') else: print('The string does NOT contain any of the specified characters')
If you need a more elegant solution, scroll down to the next subheading.

The first example uses the in operator to check if the string contains a hyphen, a dot and an @ symbol.

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.

We used the boolean and operator, so for the if block to run, all conditions have to be met.

main.py
my_str = 'bobby_hadz.com@' if ('_' in my_str) and ('.' in my_str) and ('@' in my_str): # 👇️ this runs print('The string contains the specified characters') else: print('The string does NOT contain the specified characters') print('_' in my_str) # 👉️ True print('!' not in my_str) # 👉️ True

If you need to check if the string contains one of the specified characters, use the or boolean operator instead.

main.py
my_str = 'bobbyhadz.com' if ('_' in my_str) or ('.' in my_str) or ('@' in my_str): # 👇️ this runs print('The string contains at least one of the specified characters') else: print('The string does NOT contain any of the specified characters')

For the if block to run at least one of the conditions has to be met.

Alternatively, you can use the all() or any() functions.

Check a string for specific characters using all() and any() #

To check a string for specific characters:

  1. Use a generator expression to iterate over the characters.
  2. Check if each character is contained in the string.
  3. If the condition is met for all characters, the string contains all of the specified characters.
main.py
# ✅ check if all of the specified characters are contained in the string def contains_all(string, characters): return all((char in string) for char in characters) print(contains_all('a_b.1-2', '_.-')) # 👉️ True print(contains_all('a_b.1-2', '_.$')) # 👉️ False # --------------------------------------------- # ✅ check if at least one the specified characters is contained in the string def contains_some(string, characters): return any((char in string) for char in characters) print(contains_some('a_b.', '.x')) # 👉️ True print(contains_some('a_b.', '$@')) # 👉️ False

The first example uses the all() function to check if all of the specified characters are contained in the string.

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

main.py
my_str = 'bobby_hadz.com@' def contains_all(string, characters): return all((char in string) for char in characters) print(contains_all('a_b.1-2', '_.-')) # 👉️ True print(contains_all('a_b.1-2', '_.$')) # 👉️ False
The all() function only returns True if all of the characters are contained in the string.

If the condition returns False for any of the characters, the all() function short-circuits and returns False.

You can use the any() function if you need to check if at least one of the specified characters is contained in the string.

main.py
def contains_some(string, characters): return any((char in string) for char in characters) print(contains_some('a_b.', '.x')) # 👉️ True print(contains_some('a_b.', '$@')) # 👉️ False

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

If the condition returns True for any of the characters, the any() function short-circuits and returns True.

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.