Python Reference · String Methods
Python str.isalpha() method
Reference for Python str.isalpha(): test that a string is non-empty and contains only letters, including Unicode letters, with spaces and digits as pitfalls.
The str.isalpha() method returns True if the string is not empty and every character is a letter, as defined by Unicode. Digits, spaces, hyphens, apostrophes and punctuation all make it return False.
Letters from any alphabet qualify, so "Ünal", "π" and "日本語" are all alphabetic. Use it for quick checks and filtering, but not as a full "is this a valid name" test: names often contain spaces, hyphens and apostrophes.
print("Python".isalpha()) # True print("Python3".isalpha()) # False
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input · what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input · what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Syntax
str.isalpha()
| Name | Description |
|---|---|
| (none) | The method takes no arguments. |
Return value: True if the string has at least one character and all of them are letters (Unicode categories Lm, Lt, Lu, Ll or Lo); otherwise False.
Raises: nothing.
Usage notes
- Empty string is
False. - Unicode letters count. Accented characters and non-Latin scripts return
True. For ASCII letters only, addisascii()or usestring.ascii_letters. - Spaces fail.
"two words".isalpha()isFalse. Split first if you want to check each word. - Digits are not letters. Use
isalnum()to allow both, orisdigit()for digits. - Combining marks. A letter followed by a separate combining accent can fail, because the combining mark is not a letter. Normalizing with
unicodedata.normalize("NFC", s)first often fixes this. - Not a language check. It does not tell you which alphabet the text is written in.
- Single characters.
ch.isalpha()is the usual filter inside a comprehension for counting or stripping non-letters.