I am new in python and I am having some problems with the isaplha() function. I would like to check whether in a variable some of the characters are letters. I have implemented a for loop that goes through all the character and check whether they are letters or not.
variable = '23 Uhr'
for character in variable:
is_letter = variable.isalpha()
print(character, is_letter)
The problem is that it doesn't distinguish between letters and digits and gives me 'False' as output for every character (even if it is actually a letter!). I have also tried to use the isdigit() function to verify whether it can distinguish numbers, but also in this case it always returns me 'False'. I think it is a problem that has to do with how the variable is stored. The value that I am using, indeed, is part of a bigger dataframe, but when I use the type() function, it tells me that is a part of the class string. I don't know how to solve the problem. Thank you !
You have a logical error on your code. Instead of
is_letter = variable.isalpha()
it should be
is_letter = character.isalpha()
Working code:
>>> variable = '23 Uhr'
>>> for character in variable:
... is_letter = character.isalpha()
... print(character, is_letter)
...
2 False
3 False
False
U True
h True
r True