例如:
检查unicode对象中的所有字符是否都是十进制数:
txt = "\u0033" #unicode for 3x = txt.isdecimal()print(x)
1、定义和用法
如果所有字符均为数字(0-9),则isdecimal()
方法将返回True。
此方法也适用于unicode字符对象。
2、调用语法
string.isdecimal()
3、参数说明
没有参数。
4、isdigit() 、isnumeric()、isdecimal() 的区别
1)区别
数字类型 | 函数 | 能否判别 |
---|---|---|
unicode(半角) | isdigit() isnumeric() isdecimal() | True True True |
全角数字 | isdigit() isnumeric() isdecimal() | True True True |
bytes数字 | isdigit() isnumeric() isdecimal() | True False False |
阿拉伯数字 | isdigit() isnumeric() isdecimal() | False True False |
汉字数字 | isdigit() isnumeric() isdecimal() | False True False |
2)示例代码
num = "1" #unicodeprint(num.isdigit()) # Trueprint(num.isdecimal()) # Trueprint(num.isnumeric()) # Truenum = "1" # 全角print(num.isdigit()) # Trueprint(num.isdecimal()) # Trueprint(num.isnumeric()) # Truenum = b"1" # byteprint(num.isdigit()) # Trueprint(num.isdecimal()) # AttributeError ‘bytes’ object has no attribute ‘isdecimal’print(num.isnumeric()) # AttributeError ‘bytes’ object has no attribute ‘isnumeric’num = "IV" # 罗马数字print(num.isdigit()) # Trueprint(num.isdecimal()) # Falseprint(num.isnumeric()) # Truenum = "四" # 汉字print(num.isdigit()) # Falseprint(num.isdecimal()) # Falseprint(num.isnumeric()) # True
5、使用示例
例如:
检查Unicode中的所有字符是否都是十进数:
a = "\u0030" #unicode for 0b = "\u0047" #unicode for Gprint(a.isdecimal())print(b.isdecimal())