Python String isalpha()

Filed Under: Python

Python String isalpha() function returns True if all the characters in the string are alphabets, otherwise False. If the string is empty then this function returns False.

python string isalpha()

Python String isalpha()

Alphabetic characters are those characters defined in the Unicode character database as “Letter”, i.e., those with general category property being one of “Lm”, “Lt”, “Lu”, “Ll”, or “Lo”.

Let’s look at some of the examples of isalpha() function.


s = 'Hello'

print(s.isalpha())

Output: True because all the characters in the string are alphabets.


s = '123'
print(s.isalpha())

Output: False because the string contains numbers.


s = ''
print(s.isalpha())

Output: False because it’s an empty string.


s = 'çå'
print(s.isalpha())

Output: True because ç and å are defined as letters in the Unicode character database.

Printing all Alpha characters in Python

We can use unicode module to check if a character is alpha or not. Here is the program to print all the alpha Unicode characters.


import unicodedata

count = 0
for codepoint in range(2 ** 16):
    ch = chr(codepoint)
    if ch.isalpha():
        print(u'{:04x}: {} ({})'.format(codepoint, ch, unicodedata.name(ch, 'UNNAMED')))
        count = count + 1
print(f'Total Number of Alpha Unicode Characters = {count}')

Output:


...
ffd7: ᅲ (HALFWIDTH HANGUL LETTER YU)
ffda: ᅳ (HALFWIDTH HANGUL LETTER EU)
ffdb: ᅴ (HALFWIDTH HANGUL LETTER YI)
ffdc: ᅵ (HALFWIDTH HANGUL LETTER I)
Total Number of Alpha Unicode Characters = 48832

I have provided only partial output because the number of alpha Unicode characters is huge.

You can checkout more Python examples from our GitHub Repository.

Reference: Official Documentation, Unicode Database Latin-1 Supplement

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages