Python中isdigit、isnumeric、isdecimal,


isdigit

字符串的isdigit方法用于判断字符串是否只包含数字,即0-9的字符

print('1233'.isdigit()) # True
print('12.33'.isdigit()) # False

  

isnumeric

字符串的isnumeric方法可用于判断字符串是否是数字,数字包括Unicode数字,byte数字(单字节),全角数字(双字节),罗马数字

print('23'.isnumeric()) # True
print('五十五'.isnumeric()) # True
print('Ⅵ'.isnumeric()) # True
print("12345".isnumeric()) # True

  

isdecimal

字符串的isdecimal方法检查字符串是否只包含十进制字符(Unicode数字,,全角数字(双字节))
一个字符串中包含除十进制数字之外的字符,如空字符串、空格、标点符号、小数点等字符都会认为为False.

print('1233'.isdecimal()) # True
#Python学习交流群:711312441
print('12.33'.isdecimal()) # False
print("0b1011".isdecimal()) # 二进制 False
print("0o17".isdecimal()) # 八进制 False
print("0x9F".isdecimal()) # 十六进制 False
print("12345".isdecimal()) # 全角数字 True
print("2/3".isdecimal()) # 分数 False
print("①②③".isdecimal()) # 汉字数字 False
print("ⅠⅡⅢ".isdecimal()) # 罗马数字 False
print("2/3".isdecimal()) # 分数 False

评论关闭