str.isnumeric(): True if 只包括数字;otherwise False。注重:此函数只能用于unicode string
str.isdigit(): True if 只包括数字;otherwise False。
str.isalpha():True if 只包括字母;otherwise False。
str.isalnum():True if 只包括字母或许数字;otherwise False。
示例字符串:
str_1 = "123"
str_2 = "Abc"
str_3 = "123Abc"
代码处置惩罚历程:
#用isdigit函数推断是不是数字 print(str_1.isdigit()) Ture print(str_2.isdigit()) False print(str_3.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
严厉剖析:有除了数字或许字母外的标记(空格,分号,etc.)都邑False
isalnum()必需是数字和字母的夹杂
isalpha()不辨别大小写
以上就是怎样推断一个字符串是数字的细致内容,更多请关注ki4网别的相干文章!