python判断字符串,str函数isdigit、isdecimal、isnumeric的区别
s为字符串
s.isalnum() 所有字符都是数字或者字母
s.isalpha() 所有字符都是字母
s.isdigit() 所有字符都是数字
s.islower() 所有字符都是小写
s.isupper() 所有字符都是大写
s.istitle() 所有单词都是首字母大写,像标题
s.isspace() 所有字符都是空白字符、\t、\n、\r
判断是整数还是浮点数
a=123
b=123.123
>>>isinstance(a,int)
True
>>>isinstance(b,float)
True
>>>isinstance(b,int)
False
num = "1" #unicodenum.isdigit() # Truenum.isdecimal() # Truenum.isnumeric() # True num = "1" # 全角num.isdigit() # Truenum.isdecimal() # Truenum.isnumeric() # True num = b"1" # bytenum.isdigit() # Truenum.isdecimal() # AttributeError 'bytes' object has no attribute 'isdecimal'num.isnumeric() # AttributeError 'bytes' object has no attribute 'isnumeric' num = "IV" # 罗马数字num.isdigit() # Truenum.isdecimal() # Falsenum.isnumeric() # True num = "四" # 汉字num.isdigit() # Falsenum.isdecimal() # Falsenum.isnumeric() # True ===================isdigit()True: Unicode数字,byte数字(单字节),全角数字(双字节),罗马数字False: 汉字数字Error: 无 isdecimal()True: Unicode数字,,全角数字(双字节)False: 罗马数字,汉字数字Error: byte数字(单字节) isnumeric()True: Unicode数字,全角数字(双字节),罗马数字,汉字数字False: 无Error: byte数字(单字节) ================import unicodedata unicodedata.digit("2") # 2unicodedata.decimal("2") # 2unicodedata.numeric("2") # 2.0 unicodedata.digit("2") # 2unicodedata.decimal("2") # 2unicodedata.numeric("2") # 2.0 unicodedata.digit(b"3") # TypeError: must be str, not bytesunicodedata.decimal(b"3") # TypeError: must be str, not bytesunicodedata.numeric(b"3") # TypeError: must be str, not bytes unicodedata.digit("Ⅷ") # ValueError: not a digitunicodedata.decimal("Ⅷ") # ValueError: not a decimalunicodedata.numeric("Ⅷ") # 8.0 unicodedata.digit("四") # ValueError: not a digitunicodedata.decimal("四") # ValueError: not a decimalunicodedata.numeric("四") # 4.0 #"〇","零","一","壱","二","弐","三","参","四","五","六","七","八","九","十","廿","卅","卌","百","千","万","万","亿"
转载于:https://www.cnblogs.com/xulian/p/9354580.html
python判断字符串,str函数isdigit、isdecimal、isnumeric的区别相关推荐
- python中str函数isdigit、isdecimal、isnumeric的区别
python中str函数isdigit.isdecimal.isnumeric的区别 num = "1" #unicode num.isdigit() # True num. ...
- Python判断字符串是否为数字
文章目录 问题描述 解决方案 修改内置类型 百分号字符串转数字 参考文献 问题描述 本文版本为 Python 3.8 str.isdigit() 仅可判断非负整数 print('0'.isdigit( ...
- python字符串find函数-python常见字符串处理函数与用法汇总
本文实例讲述了python常见字符串处理函数与用法.分享给大家供大家参考,具体如下: 1.find 作用:在一个较长字符串中查找子串.返回子串所在位置的最左端索引,如果没有找到则返回-1.如果指定 b ...
- python判断字符串
python判断字符串 s为字符串 s.isalnum() 所有字符都是数字或者字母 s.isalpha() 所有字符都是字母 s.isdigit() 所有字符都是数字 s.islower() 所有字 ...
- python判断字符串是纯数字_python判断字符串是否纯数字的方法
本文实例讲述了python判断字符串是否纯数字的方法.分享给大家供大家参考.具体如下: 判断的代码如下,通过异常判断不能区分前面带正负号的区别,正则表达式可以根据自己需要比较灵活的写,通过isdigi ...
- js字符串replace替换多个_汇总几大python常见字符串处理函数与用法(建议收藏)...
前言: 这篇文章主要介绍了python常见字符串处理函数与用法,结合实例形式详细分析了Python字符串操作函数find.join.replace及split功能.使用技巧与操作注意事项,需要的朋友可 ...
- Python:实现字符串Z 函数或 Z 算法(附完整源码)
Python:实现字符串Z 函数或 Z 算法 def z_function(input_str: str) -> list[int]:z_result = [0 for i in range(l ...
- Python 判断数据类型的函数
#Python 判断数据类型的函数 可使用内置函数 isinstance() 函数来判断一个对象是否是一个已知的类型,类似 type(). 举例1 >>>arg=1234567 &g ...
- python 判断字符串中字符类型的常用方法
python 判断字符串中字符类型组成常用的方法 startswith() 检查字符串是否以设定内容开头,默认是整个字符串,如果是则返回True 否则则返回False str1='hello' pri ...
最新文章
- 教师教学的“五个转化”能力
- php lang无效,详解 Go 中的不可变类型
- python输出数据到excel-使用python将大量数据导出到Excel中的小技巧分享
- erlang精要(7)-模块
- Java FlatMapFunction in Spark: error: is not abstract and does not override abstract method解决方案
- SSD硬盘 全盘安全擦除
- 机器学习十大经典算法之逻辑回归
- 【经典精读】Transformer模型深度解读
- 5.PHP 命令行模式
- (二)docker常用命令
- java套接字通信_Java网络通信套接字 | 学步园
- 渐开线曲线方程c语言,proe常用曲线方程大全
- sql server 2008r2安装注意的问题
- rgb sw 线主板接口在哪_火力升级 华硕B460重炮手PRO主板豪横上市_搜狐汽车
- Hexo博客摘要生成方法
- vue2使用element UI中Descriptions组件的遍历问题
- 怎么搭建百度网盘目录搜索网站?网盘目录#百度网盘
- 军品研制过程评审活动-(一)论证阶段
- 一款经典动漫改编的二次元日系角色扮演类手游——航海王热血航线
- File ignored because of a matching ignore pattern. Use “--no-ignore“ to override