阅读 python 文档我了解到 .isdecimal() 和 .isdigit() 字符串函数,但我没有发现文献对它们的可用区别太清楚。有人可以向我提供这两个函数的区别的代码示例吗?
类似行为:
>>> str.isdecimal('1')
True
>>> str.isdigit('1')
True
>>> str.isdecimal('1.0')
False
>>> str.isdigit('1.0')
False
>>> str.isdecimal('1/2')
False
>>> str.isdigit('1/2')
False
There are differences, but they're somewhat rare*. It mainly crops up with various unicode characters, such as 2
:
>>> c = '\u00B2'
>>> c.isdecimal()
False
>>> c.isdigit()
True
您还可以使用以下命令进一步深入仔细区分 unicode 的兔子洞:isnumeric
method:
>>> c = '\u00BD' # ½
>>> c.isdecimal()
False
>>> c.isdigit()
False
>>> c.isnumeric()
True
*At least, I've never encountered production code that needs to distinguish between strings that contain different types of these exceptional situations, but surely use cases exist somewhere.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)