How can I detect either numbers or letters in a string? I am aware you use the ASCII codes, but what functions take advantage of them?
2 Answers
Check if string is nonnegative digit (integer) and alphabet
You may use str.isdigit() and str.isalpha() to check whether a given string is a nonnegative integer (0 or greater) and alphabetical character, respectively.
Sample Results:
# For alphabet
>>> 'A'.isdigit()
False
>>> 'A'.isalpha()
True
# For digit
>>> '1'.isdigit()
True
>>> '1'.isalpha()
False