相关的方法有
isdigit
()
,
isdecimal
()
,
isnumeric
()。
isdigit()
Python isdigit() 方法检测字符串是否只由数字组成,如果字符串只包含数字则返回 True 否则返回 False。
isdecimal()
Python isdecimal() 方法检查字符串是否只包含十进制字符。这种方法只存在于unicode对象。如果字符串是否只包含十进制字符返回True,否则返回False。
isnumeric()
Python isnumeric() 方法检测字符串是否只由数字组成。这种方法是只针对unicode对象。如果字符串中只包含数字字符,则返回 True,否则返回 False
Str1 = 'Hello world'
Str2 = '2021'
Str3 = "CSDN2021"
print(Str1.isdigit())
print(Str2.isdigit())
print(Str2.isdecimal())
print(Str3.isnumeric())
print('-------------')
# isnumeric() 会 认 为 是 True
Str4='贰拾'
print(Str4.isdigit())
print(Str4.isdecimal())
print(Str4.isnumeric())
print('-------------')
Str5 = '二十'
print(Str4.isdigit())
print(Str4.isdecimal())
print(Str4.isnumeric())
结果: