Saturday, 16 July 2016

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



So I know about islower and isupper, but i can't seem to find out if you can check whether or not that character is a letter?



Example:

s = 'abcdefg'

s2 = '123abcd'
s3 = 'abcDEFG'

s[0].islower() = True
s2[0].islower()= False
s3[0].islower()=True


is there any way to just ask if it is a character besides doing .islower() or .isupper() ?


Answer




You can use str.isalpha().



For example:



s = 'a123b'

for char in s:
print(char, char.isalpha())



Output:



a True
1 False
2 False
3 False
b True

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...