Check if a string contains any letters in Python

avatar

Borislav Hadzhiev

Last updated: Sep 18, 2022

banner

Photo from Unsplash

Check if a string contains any letters in Python #

Use the re.search() method to check if a string contains any letters, e.g. re.search(r'[a-zA-Z]', string). The re.search() method will return a match object if the string contains any letters, otherwise None is returned.

main.py
import re def contains_any_letters(string): return re.search(r'[a-zA-Z]', string) if contains_any_letters('bobby123'): # 👇️ this runs print('✅ The string contains a letter') else: print('⛔️ The string does NOT contain any letters') # 👇️ <re.Match object; span=(0, 1), match='b'> print(contains_any_letters('bobbyhadz')) print(contains_any_letters(' ')) # 👉️ None print(contains_any_letters('')) # 👉️ None print(contains_any_letters('123')) # 👉️ None print(contains_any_letters('!@#')) # 👉️ None
If you are looking for a non-regex solution, scroll down to the next subheading.

We used the re.search() method to check if a string contains any letters.

The re.search method looks for the first location in the string where the provided regular expression produces a match.

The first argument we passed to the method is a regex.

The square brackets [] are used to indicate a set of characters.

The a-z and A-Z characters represent lowercase and uppercase ranges of letters.

If the re.search() method finds a match, it will return a match object, otherwise None is returned.

main.py
import re def contains_any_letters(string): return re.search(r'[a-zA-Z]', string) # 👇️ <re.Match object; span=(0, 1), match='b'> print(contains_any_letters('bobbyhadz')) print(contains_any_letters(' ')) # 👉️ None print(contains_any_letters('')) # 👉️ None print(contains_any_letters('123')) # 👉️ None print(contains_any_letters('!@#')) # 👉️ None

Alternatively, you can use the any() function.

Check if a string contains any letters using any() #

To check if a string contains any letters:

  1. Use a generator expression to iterate over the string.
  2. Use the str.isalpha() method to check if each character is a letter.
  3. If the condition is met, the string contains at least one letter.
main.py
def contains_any_letters(string): return any(char.isalpha() for char in string) if contains_any_letters('bobby hadz'): # 👇️ this runs print('✅ The string contains a letter') else: print('⛔️ The string does NOT contain any letters') print(contains_any_letters('bobbyhadz')) # 👉️ True print(contains_any_letters('фф вв бб')) # 👉️ True print(contains_any_letters(' ')) # 👉️ False print(contains_any_letters('')) # 👉️ False print(contains_any_letters('123')) # 👉️ False print(contains_any_letters('!@#')) # 👉️ False

We used a generator expression to iterate over the string.

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

On each iteration, we use the str.isalpha() method to check if the current character is a letter and return the result.

The str.isalpha() method returns True if all characters in the string are alphabetic and there is at least one character, otherwise False is returned.

main.py
print('BOBBY'.isalpha()) # 👉️ True # 👇️ contains space print('BOBBY HADZ'.isalpha()) # 👉️ False

The str.isalpha method considers alphabetic characters the ones that are defined in the Unicode character database as "Letter".

If you want to check if a string contains any ASCII letters, use the regex solution instead.

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

main.py
def contains_any_letters(string): return any(char.isalpha() for char in string)

If the iterable is empty or none of the elements in the iterable are truthy, the any function returns False.

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

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.