码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • isdigit isdecimal isnumeric 区别


    一、代码测试

    1. num = "1" #unicode
    2. num.isdigit() # True
    3. num.isdecimal() # True
    4. num.isnumeric() # True
    5. num = "1" # 全角
    6. num.isdigit() # True
    7. num.isdecimal() # True
    8. num.isnumeric() # True
    9. num = b"1" # byte
    10. num.isdigit() # True
    11. num.isdecimal() # AttributeError 'bytes' object has no attribute 'isdecimal'
    12. num.isnumeric() # AttributeError 'bytes' object has no attribute 'isnumeric'
    13. num = "IV" # 罗马数字
    14. num.isdigit() # True
    15. num.isdecimal() # False
    16. num.isnumeric() # True
    17. num = "四" # 汉字
    18. num.isdigit() # False
    19. num.isdecimal() # False
    20. num.isnumeric() # True

    二、总结

    isdigit()
    True: Unicode数字,byte数字(单字节),全角数字(双字节),罗马数字
    False: 汉字数字
    Error: 无
     
    isdecimal()
    True: Unicode数字,,全角数字(双字节)
    False: 罗马数字,汉字数字
    Error: byte数字(单字节)
     
    isnumeric()
    True: Unicode数字,全角数字(双字节),罗马数字,汉字数字
    False: 无
    Error: byte数字(单字节)

    三、源码分析

    __init__.py 

    1. def isdecimal(self):
    2. return self.data.isdecimal()
    3. def isdigit(self):
    4. return self.data.isdigit()
    5. def isnumeric(self):
    6. return self.data.isnumeric()

     builtins.py

    1. def isdecimal(self, *args, **kwargs): # real signature unknown
    2. """
    3. Return True if the string is a decimal string, False otherwise.
    4. A string is a decimal string if all characters in the string are decimal and
    5. there is at least one character in the string.
    6. 如果字符串中的所有字符都是十进制和,则该字符串为十进制字符串,字符串中至少有一个字符。
    7. """
    8. pass
    9. def isdigit(self, *args, **kwargs): # real signature unknown
    10. """
    11. Return True if the string is a digit string, False otherwise.
    12. A string is a digit string if all characters in the string are digits and there
    13. is at least one character in the string.
    14. 如果字符串中的所有字符都是数字,且字符串中至少有一个字符,则该字符串为数字字符串。
    15. """
    16. pass
    17. def isnumeric(self, *args, **kwargs): # real signature unknown
    18. """
    19. Return True if the string is a numeric string, False otherwise.
    20. A string is numeric if all characters in the string are numeric and there is at
    21. least one character in the string.
    22. 如果字符串中的所有字符都是数字,且字符串中至少有一个字符,则该字符串为数值型。
    23. """
    24. pass

     

  • 相关阅读:
    LeetCode --- 1417. Reformat The String 解题报告
    【API封装接口的应用】大数据值得深思的十二个典型应用案例,和未来机遇畅想
    使用VisualStudio生成类图结构图for高效阅读代码
    C++菱形继承问题与虚拟继承原理
    第二周 day03
    新库上线 | CnOpenData电力、热力、燃气及水生产和供应业工商注册企业基本信息数据
    can 接口调试am3352
    《DevOps 精要:业务视角》- 读书笔记(四)
    Java设计模式-建造者模式
    Bootstrap前端开发框架(简介、版本、优点、使用、布局容器、栅格系统(栅格选项参数,列嵌套,列偏移,列排序,响应式工具))
  • 原文地址:https://blog.csdn.net/c_lanxiaofang/article/details/127956632
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号