python中isdigit()、isdecimal()和isnumeric的区别!

举报
tea_year 发表于 2021/12/30 00:19:16 2021/12/30
【摘要】 isdecimal(...) | S.isdecimal() -> bool | | Return True if there are only decimal characters in S, | False otherwise. 翻译:如果S中只有十进制字符,则返回True,否则为F...

  
  1. isdecimal(...)
  2. | S.isdecimal() -> bool
  3. |
  4. | Return True if there are only decimal characters in S,
  5. | False otherwise.
  6. 翻译:如果S中只有十进制字符,则返回True,否则为False。

  
  1. isdigit(...)
  2. | S.isdigit() -> bool
  3. |
  4. | Return True if all characters in S are digits
  5. | and there is at least one character in S, False otherwise.
  6. 翻译:如果S中的所有字符都是数字,并且在S中至少有一个字符,则返回True。

  
  1. isnumeric(...)
  2. | S.isnumeric() -> bool
  3. |
  4. | Return True if there are only numeric characters in S,
  5. | False otherwise.
  6. 翻译:如果S中只有数字字符,则返回True,否则为False。

  
  1. 1 s = '123'
  2. 2 print(s.isdigit())
  3. 3 print(s.isdecimal())
  4. 4 print(s.isnumeric())

结果为:


  
  1. True
  2. True
  3. True

  
  1. s = b'123'
  2. print(s.isdigit())
  3. #print(s.isdecimal())
  4. #print(s.isnumeric())

结果为: (只有第一个能正常输出,另外两个报属性错误)

True
 

  
  1. ---------------------------------------------------------------------------
  2. AttributeError Traceback (most recent call last)
  3. <ipython-input-19-9e3f7cdf9524> in <module>()
  4. 2 print(s.isdigit())
  5. 3 #print(s.isdecimal())
  6. ----> 4 print(s.isnumeric())
  7. AttributeError: 'bytes' object has no attribute 'isnumeric'

  
  1. s = '123.0'
  2. print(s.isdigit())
  3. print(s.isdecimal())
  4. print(s.isnumeric())

  
  1. False
  2. False
  3. False

  
  1. s = '三叁'
  2. print(s.isdigit())
  3. print(s.isdecimal())
  4. print(s.isnumeric())

  
  1. False
  2. False
  3. True

  
  1. s = 'Ⅲ'
  2. print(s.isdigit())
  3. print(s.isdecimal())
  4. print(s.isnumeric())

  
  1. False
  2. False
  3. True
  4. 总结:

  
  1. isdigit()
  2. True: Unicode数字,byte数字(单字节),全角数字(双字节)
  3. False: 汉字数字,罗马数字,小数
  4. Error: 无
  5. isdecimal()
  6. True: Unicode数字,全角数字(双字节)
  7. False: 罗马数字,汉字数字,小数
  8. Error: byte数字(单字节)

  
  1. isnumeric()
  2. True: Unicode数字,全角数字(双字节),罗马数字,汉字数字
  3. False: 小数
  4. Error: byte数字(单字节)

文章来源: aaaedu.blog.csdn.net,作者:tea_year,版权归原作者所有,如需转载,请联系作者。

原文链接:aaaedu.blog.csdn.net/article/details/81630631

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200