python中isdigit()、isdecimal()和isnumeric的区别!
【摘要】
isdecimal(...) | S.isdecimal() -> bool | | Return True if there are only decimal characters in S, | False otherwise. 翻译:如果S中只有十进制字符,则返回True,否则为F...
-
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。
-
1 s = '123'
-
2 print(s.isdigit())
-
3 print(s.isdecimal())
-
4 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
-
-
总结:
-
isdigit()
-
True: Unicode数字,byte数字(单字节),全角数字(双字节)
-
False: 汉字数字,罗马数字,小数
-
Error: 无
-
-
isdecimal()
-
True: Unicode数字,全角数字(双字节)
-
False: 罗马数字,汉字数字,小数
-
Error: byte数字(单字节)
-
isnumeric()
-
True: Unicode数字,全角数字(双字节),罗马数字,汉字数字
-
False: 小数
-
Error: byte数字(单字节)
文章来源: aaaedu.blog.csdn.net,作者:tea_year,版权归原作者所有,如需转载,请联系作者。
原文链接:aaaedu.blog.csdn.net/article/details/81630631
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)