TriskelionKal TriskelionKal - 1 month ago 6
Python Question

Detect whether a Python string is a number or a letter

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?

Answer

You may use str.isdigit() and str.isalpha() to find this.

Sample Results:

# For alphabet
>>> 'A'.isdigit()
False
>>> 'A'.isalpha()
True

# For digit
>>> '1'.isdigit()
True
>>> '1'.isalpha()
False