How to check if a character in a string is a letter in Python?



You can use the isalpha() method from string class. It checks if a string consists only of alphabets. You can also use it to check if a character is an alphabet or not. For example, if you want to check if char at 5th index is letter or not,

>>> s = "Hello people"
>>> s[4].isalpha()
True

You can also check whole strings, if they are alphabetic or not. For example,

>>> s = "Hello people"
>>> s.isalpha()
False
>>> "Hello".isalpha()
True

Advertisements