The quickest way to check whether a character in a string is a letter is to use the str.isalpha() function.
1 2 |
character = 'a' print(character.isalpha()) |
This code is going to return True because ‘a’ is a letter.
Check all characters inside a string
If you want to check the entire string for alphanumerical characters, you can use this code:
1 2 3 4 |
string = 'Year 1984!' for character in string: print(character, character.isalpha()) |
If you run it, it’s going to return the following output:
Y True e True a True r True False 1 False 9 False 8 False 4 False ! False
Only the first four characters inside the string are letters, and the result shows us that.