0
点赞
收藏
分享

微信扫一扫

isdigit isdecimal isnumeric 区别


一、代码测试

num = "1"  #unicode
num.isdigit() # True
num.isdecimal() # True
num.isnumeric() # True

num = "1" # 全角
num.isdigit() # True
num.isdecimal() # True
num.isnumeric() # True

num = b"1" # byte
num.isdigit() # True
num.isdecimal() # AttributeError 'bytes' object has no attribute 'isdecimal'
num.isnumeric() # AttributeError 'bytes' object has no attribute 'isnumeric'

num = "IV" # 罗马数字
num.isdigit() # True
num.isdecimal() # False
num.isnumeric() # True

num = "四" # 汉字
num.isdigit() # False
num.isdecimal() # False
num.isnumeric() # True

二、总结

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

三、源码分析

__init__.py 

def isdecimal(self):
return self.data.isdecimal()

def isdigit(self):
return self.data.isdigit()

def isnumeric(self):
return self.data.isnumeric()

 builtins.py

def isdecimal(self, *args, **kwargs): # real signature unknown
"""
Return True if the string is a decimal string, False otherwise.

A string is a decimal string if all characters in the string are decimal and
there is at least one character in the string.
如果字符串中的所有字符都是十进制和,则该字符串为十进制字符串,字符串中至少有一个字符。
"""
pass


def isdigit(self, *args, **kwargs): # real signature unknown
"""
Return True if the string is a digit string, False otherwise.

A string is a digit string if all characters in the string are digits and there
is at least one character in the string.
如果字符串中的所有字符都是数字,且字符串中至少有一个字符,则该字符串为数字字符串。
"""
pass


def isnumeric(self, *args, **kwargs): # real signature unknown
"""
Return True if the string is a numeric string, False otherwise.

A string is numeric if all characters in the string are numeric and there is at
least one character in the string.
如果字符串中的所有字符都是数字,且字符串中至少有一个字符,则该字符串为数值型。
"""
pass

isdigit isdecimal isnumeric 区别_ico

 

举报

相关推荐

0 条评论