1、函数介绍
isdecimal(...)
| S.isdecimal() -> bool
|
| Return True if there are only decimal characters in S,
| False otherwise.
翻译:如果S中只有十进制字符,则返回True,否则为False。
isdigit(...)
| S.isdigit() -> bool
|
| Return True if all characters in S are digits
| and there is at least one character in S, False otherwise.
翻译:如果S中的所有字符都是数字,并且在S中至少有一个字符,则返回True。
isnumeric(...)
| S.isnumeric() -> bool
|
| Return True if there are only numeric characters in S,
| False otherwise.
翻译:如果S中只有数字字符,则返回True,否则为False。
2、例程
s = '123'
print(s.isdigit())
print(s.isdecimal())
print(s.isnumeric())
结果为:
True
True
True
s = b'123'
print(s.isdigit())
#print(s.isdecimal())
#print(s.isnumeric())
结果为: (只有第一个能正常输出,另外两个报属性错误)
True
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-19-9e3f7cdf9524> in <module>()
2 print(s.isdigit())
3 #print(s.isdecimal())
----> 4 print(s.isnumeric())
AttributeError: 'bytes' object has no attribute 'isnumeric'
s = '123.0'
print(s.isdigit())
print(s.isdecimal())
print(s.isnumeric())
False
False
False
s = '三叁'
print(s.isdigit())
print(s.isdecimal())
print(s.isnumeric())
False
False
True
s = 'Ⅲ'
print(s.isdigit())
print(s.isdecimal())
print(s.isnumeric())
False
False
True
3、总结:
isdigit()
True: Unicode数字,byte数字(单字节),全角数字(双字节)
False: 汉字数字,罗马数字,小数
Error: 无
isdecimal()
True: Unicode数字,全角数字(双字节)
False: 罗马数字,汉字数字,小数
Error: byte数字(单字节)
isnumeric()
True: Unicode数字,全角数字(双字节),罗马数字,汉字数字
False: 小数
Error: byte数字(单字节)
转载自:
https://blog.youkuaiyun.com/zhangchen124/article/details/81630631
本文详细介绍了Python中用于判断字符串是否全部由数字组成的三种方法:isdigit(), isdecimal(), 和 isnumeric()。通过实例对比了它们在不同情况下的应用效果,并总结了各自的特点及适用范围。
1348

被折叠的 条评论
为什么被折叠?



