Check if a character in a String is a Letter in Python

avatar

Borislav Hadzhiev

Last updated: Sep 18, 2022

banner

Photo from Unsplash

Check if a character in a String is a Letter in Python #

Use the str.isalpha() method to check if a character in a string is a letter, e.g. is_letter = my_str[0].isalpha(). The isalpha() method will return True if the character is a letter and False otherwise.

main.py
my_str = 'bo12' for char in my_str: # b True # o True # 1 False # 2 False print(char, char.isalpha()) is_letter = my_str[0].isalpha() print(is_letter) # ๐Ÿ‘‰๏ธ True is_letter = my_str[-1].isalpha() print(is_letter) # ๐Ÿ‘‰๏ธ False

We used the str.isalpha() method to check if a character in a string is a letter.

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

You can access the character at a specific index when calling isalpha().

main.py
my_str = 'bo12' is_letter = my_str[0].isalpha() print(is_letter) # ๐Ÿ‘‰๏ธ True if is_letter: # ๐Ÿ‘‡๏ธ this runs print('The character is a letter') else: print('The character is not a letter') is_letter = my_str[-1].isalpha() print(is_letter) # ๐Ÿ‘‰๏ธ False
Python indexes are zero-based, so the first character in a string has an index of 0, and the last character has an index of -1 or len(my_str) - 1.

You can also use a for loop to iterate over the string and check if each character is a letter.

main.py
my_str = 'bo12' for char in my_str: # b True # o True # 1 False # 2 False print(char, char.isalpha())

The str.isalpha() method returns True for many alphabetic characters defined in the Unicode character database.

main.py
print('ะค'.isalpha()) # ๐Ÿ‘‰๏ธ True
If you need to check if a character is an ASCII (a-z A-Z) letter, use thestring.ascii_letters attribute.
main.py
import string # ๐Ÿ‘‡๏ธ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ print(string.ascii_letters) my_str = 'boั„ัŒ' print(my_str[0] in string.ascii_letters) # ๐Ÿ‘‰๏ธ True print(my_str[2] in string.ascii_letters) # ๐Ÿ‘‰๏ธ False if my_str[0] in string.ascii_letters: # ๐Ÿ‘‡๏ธ this runs print('The character is an ASCII letter') else: print('The character is NOT an ASCII letter')

The examples check if a specific character is an ASCII letter.

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.

If you need to check if all characters in a string are letters, use the all() function.

main.py
my_str = 'bobbyhadz' all_letters = all(char.isalpha() for char in my_str) print(all_letters) # ๐Ÿ‘‰๏ธ True if all_letters: # ๐Ÿ‘‡๏ธ this runs print('All characters in the string are letters') else: print('Not all characters in the string are letters')

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

The all() function returns True for empty strings.

If you need to handle this scenario, check for the string's length.

main.py
my_str = '' all_letters = (len(my_str) > 0 and all(char.isalpha() for char in my_str)) print(all_letters) # ๐Ÿ‘‰๏ธ False

We used the boolean and operator, so for the expression to return True both conditions have to be met.

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.

Copyright ยฉ 2022 Borislav Hadzhiev