Check Python String for Letters or Numbers
You can use the isalpha() method and isdigit() method to check if a string consists only of letters or digits. Here is an example code:
def is_alpha_or_digit(s):
return s.isalpha() or s.isdigit()
# 测试
print(is_alpha_or_digit("hello123")) # False
print(is_alpha_or_digit("hello")) # True
print(is_alpha_or_digit("123")) # True
In the example above, the function is_alpha_or_digit() takes a string as a parameter, and then uses the isalpha() method and the isdigit() method to determine if the string consists entirely of alphabetic characters or digits. If the string is all alphabetic characters or digits, it returns True; otherwise, it returns False.