python中decimal函数用法_Python 字符串 isdecimal() 方法

例如:

检查unicode对象中的所有字符是否都是十进制数:txt = "\u0033" #unicode for 3

x = 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" #unicode

print(num.isdigit()) # True

print(num.isdecimal()) # True

print(num.isnumeric()) # True

num = "1" # 全角

print(num.isdigit()) # True

print(num.isdecimal()) # True

print(num.isnumeric()) # True

num = b"1" # byte

print(num.isdigit()) # True

print(num.isdecimal()) # AttributeError ‘bytes’ object has no attribute ‘isdecimal’

print(num.isnumeric()) # AttributeError ‘bytes’ object has no attribute ‘isnumeric’

num = "IV" # 罗马数字

print(num.isdigit()) # True

print(num.isdecimal()) # False

print(num.isnumeric()) # True

num = "四" # 汉字

print(num.isdigit()) # False

print(num.isdecimal()) # False

print(num.isnumeric()) # True

5、使用示例

例如:

检查Unicode中的所有字符是否都是十进数:a = "\u0030" #unicode for 0

b = "\u0047" #unicode for G

print(a.isdecimal())

print(b.isdecimal())


版权声明:本文为weixin_31678781原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。