Check if a String contains only Letters in Python

avatar

Borislav Hadzhiev

Last updated: Jul 13, 2022

banner

Photo from Unsplash

Check if a String contains only Letters in Python #

Use the str.isalpha method to check if a string contains only letters, e.g. if my_str.isalpha():. The isalpha() method will return True if the string contains only letters and False otherwise.

main.py
my_str = 'apple' if my_str.isalpha(): # πŸ‘‡οΈ this runs print('The string contains only letters') else: print('The string does NOT contain only letters')

We used the str.isalpha method to check if a string contains only alphabetic characters.

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('avocado'.isalpha()) # πŸ‘‰οΈ True print('one two'.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 only ASCII (American Standard Code for Information Interchange) characters, use the str.isascii() method.
main.py
print('зьТ'.isalpha()) # πŸ‘‰οΈ True print('зьТ'.isascii()) # πŸ‘‰οΈ False

The str.isascii method returns True if the string is empty or all characters in the string are ASCII, otherwise it returns False.

Alternatively, you can use the re.match() method.

The re.match method can also be used if you need to check if a string only contains specific letters.

main.py
import re # βœ… Check if a string only contains letters match = re.match(r'^[a-zA-Z]+$', 'apple') if match: # πŸ‘‡οΈ this runs print('The string contains only letters') else: print('The string does NOT contain only letters') # ------------------------------ # βœ… Check if a string only contains specific letters my_str = 'abc' match = re.match(r'^[abc]+$', my_str) if match: # πŸ‘‡οΈ this runs print('The string contains only letters') else: print('The string does NOT contain only letters')

The re.match method returns a match object if the provided regular expression is matched in the string.

The match method returns None if the string doesn't match the regex pattern.

The first example checks if a string contains only ASCII characters.

main.py
import re match = re.match(r'^[a-zA-Z]+$', 'apple') if match: # πŸ‘‡οΈ this runs print('The string contains only letters') else: print('The string does NOT contain only letters')

The first argument we passed to the re.match method is a regular expression.

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.

The caret ^ matches the start of the string and the dollar sign $ matches the end of the string.

The plus + causes the regular expression to match 1 or more repetitions of the preceding character (the letter ranges).

You can also use this approach if you need to check if the string only contains certain letters.

main.py
import re my_str = 'abc' match = re.match(r'^[abc]+$', my_str) if match: # πŸ‘‡οΈ this runs print('The string contains only letters') else: print('The string does NOT contain only letters')

We only added the abc characters in the set, so the regular expression checks if the supplied string only contains the specified characters.

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.