Python determines whether the string is a number or a letter or a mixture of numbers and letters

It can be achieved by using the function that comes with the python string:
Note: str.isdigit(), the number with a negative sign will also return False

str.isdigit() # Determine whether the string is a pure number, any letters, punctuation (including spaces, minus signs) will return False
——————————————————— ———————————————————————
str.isalpha() # Determine whether the string is a pure letter (not case sensitive), there are any numbers, Punctuation marks will return False
————————————————————————————————————————————
str .isalnum() # Determine whether the string is a mixture of numbers and letters, any punctuation marks will return False

str_1 = "123"
str_2 = "Abc"
str_3 = "123Abc"
str_4 = "-123"
 
#用isdigit函数判断是否数字
print(str_1.isdigit())
Ture
print(str_2.isdigit())
False
print(str_3.isdigit())
False
print(str_4.isdigit())
False
 
#用isalpha判断是否字母
print(str_1.isalpha())    
False
print(str_2.isalpha())
Ture    
print(str_3.isalpha())    
False
 
#isalnum判断是否数字和字母的组合
print(str_1.isalnum())    
Ture
print(str_2.isalnum())
Ture
print(str_1.isalnum())    
Ture

注意:如果字符串中含有除了字母或者数字之外的字符,比如空格,也会返回False

Guess you like

Origin blog.csdn.net/weixin_44414948/article/details/114401389