isdigit、isnumeric、isdecimal

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

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

  

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

1
2
3
4
print('23'.isnumeric()) # True
print('五十五'.isnumeric()) # True
print('Ⅵ'.isnumeric()) # True
rint("12345".isnumeric()) # True

  

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

1
2
3
4
5
6
7
8
9
10
print('1233'.isdecimal()) # True
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

  

posted @   sangern  阅读(55)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示