Python 字符串 (isdigit, isalnum,isnumeric)转
Python isdigit() 方法检测字符串是否只由数字组成。
语法
isdigit()方法语法:
str.isdigit()
参数
- 无。
返回值
如果字符串只包含数字则返回 True 否则返回 False。
Python isdecimal() 方法检查字符串是否只包含十进制字符。这种方法只存在于unicode对象。
注意:定义一个十进制字符串,只需要在字符串前添加 'u' 前缀即可。
语法
isdecimal()方法语法:
str.isdecimal()
参数
- 无
返回值
如果字符串是否只包含十进制字符返回True,否则返回False。
str = u"this2009";
print str.isdecimal(); str = u"";
print str.isdecimal()
输出结果:
False
True
Python isnumeric() 方法检测字符串是否只由数字组成。这种方法是只针对unicode对象。
注:定义一个字符串为Unicode,只需要在字符串前添加 'u' 前缀即可。
语法
isnumeric()方法语法:
str.isnumeric()
参数
- 无。
返回值
如果字符串中只包含数字字符,则返回 True,否则返回 False
python中str函数isdigit、isdecimal、isnumeric的区别
num = "1" #unicode
num.isdigit() # True
num.isdecimal() # True
num.isnumeric() # True num = "1" # 全角
num.isdigit() # True
num.isdecimal() # True
num.isnumeric() # True num = b"1" # byte
num.isdigit() # True
num.isdecimal() # AttributeError 'bytes' object has no attribute 'isdecimal'
num.isnumeric() # AttributeError 'bytes' object has no attribute 'isnumeric' num = "IV" # 罗马数字
num.isdigit() # True
num.isdecimal() # False
num.isnumeric() # True num = "四" # 汉字
num.isdigit() # False
num.isdecimal() # False
num.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") # 2
unicodedata.decimal("2") # 2
unicodedata.numeric("2") # 2.0 unicodedata.digit("2") # 2
unicodedata.decimal("2") # 2
unicodedata.numeric("2") # 2.0 unicodedata.digit(b"3") # TypeError: must be str, not bytes
unicodedata.decimal(b"3") # TypeError: must be str, not bytes
unicodedata.numeric(b"3") # TypeError: must be str, not bytes unicodedata.digit("Ⅷ") # ValueError: not a digit
unicodedata.decimal("Ⅷ") # ValueError: not a decimal
unicodedata.numeric("Ⅷ") # 8.0 unicodedata.digit("四") # ValueError: not a digit
unicodedata.decimal("四") # ValueError: not a decimal
unicodedata.numeric("四") # 4.0
Python 字符串 (isdigit, isalnum,isnumeric)转的更多相关文章
- Python中判断字符串是否为数字的三个方法isdecimal 、isdigit、isnumeric的差别
isdecimal .isdigit.isnumeric这三个字符串方法都用于判断字符串是否为数字,为什么用三个方法呢?他们的差别是什么内? isdecimal:是否为十进制数字符,包括Unicode ...
- Python3中isdigit(), isdecimal(), isnumeric()的区别和字符串的常用方法
# 全部小写 string.lower() # 全部大写 string.upper() # 是否全部小写 string.islower() # 是否全部大写 string.isupper() # 首字 ...
- Python 字符串
Python访问字符串中的值 Python不支持单字符类型,单字符也在Python也是作为一个字符串使用. Python访问子字符串,可以使用方括号来截取字符串,如下实例: #!/usr/bin/py ...
- Python 字符串处理大全.
Python 字符串 字符串是Pyhton中常用的数据类型,我们可以使用引号来创建字符串 . 创建字符串很简单 , 就不说了 . Python 访问字符串中的值 鬼叔本着简洁 使用的设计目的 , 在设 ...
- python 字符串探讨
本文内容基于python3 几乎所有有用的程序都会涉及到某些文本处理,不管是解析数据还是产生输出.字符串的学习是重点中的重点,这一节将重点关注文本的操作处理,比如提取字符串,搜索,替换以及解析等.大部 ...
- python字符串常用内置方法
python字符串常用内置方法 定义: 字符串是一个有序的字符的集合,用与存储和表示基本的文本信息. python中引号中间包含的就是字符串. # s1='hello world' # s2=&quo ...
- python字符串内置方法
网上已经有很多,自己操作一遍,加深印象. dir dir会返回一个内置方法与属性列表,用字符串'a,b,cdefg'测试一下 dir('a,b,cdefg') 得到一个列表 ['__add__', ' ...
- python字符串处理内置方法一览表
python字符串处理内置方法一览表 序号 方法及描述 1 capitalize()将字符串的第一个字符转换为大写 2 center(width, fillchar) 返回一个指定的宽度 widt ...
- python字符串(string)方法整理
python中字符串对象提供了很多方法来操作字符串,功能相当丰富. print(dir(str)) [..........'capitalize', 'casefold', 'center', 'co ...
随机推荐
- Java学习03 (第一遍)
Java是面向对象的语言,函数是面向过程语言的叫法,比如C语言,在Java中一般称之为方法. 构造方法的作用是实例化对象,每个类中都有,即使不写程序也会分配一个默认无参数的构造方法. Java中都是对 ...
- work单进程群发通知 后面会增加Channel组件的分组推送以及集群推送篇章
<?phpuse Workerman\Worker;use Workerman\Lib\Timer; require_once '../../web/Workerman/Autoloader.p ...
- Elasticsearch搜索异常-------org.elasticsearch.common.io.stream.NotSerializableExceptionWrapper: parse_exception
异常问题: Caused by: org.elasticsearch.index.query.QueryShardException: Failed to parse query [LOL: Uzi和 ...
- 23种经典设计模式UML类图汇总
在这里23种经典设计模式UML类图汇总 创建型模式 1.FACTORY—追MM少不了请吃饭了,麦当劳的鸡翅和肯德基的鸡翅都是MM爱吃的东西,虽然口味有所不同,但不管你带MM去麦当劳或肯德基 ...
- CKfinder for java详解二:缩略图及图片上传的缩放
我们找到 <thumbs><enabled>true</enabled><url>�SE_URL%_thumbs/</url><dir ...
- datetime空值设置
Cause: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '' for column ...
- Mysql 用户 创建与删除(基础1)
Mysql是最流行的关系型数据库管理系统之一,由瑞典MySQL AB公司开发,目前属于Oracle公司. MySQL是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数据放在一个 ...
- PHPUnit 组织测试
首先是目录结构 源文件夹为 src/ 测试文件夹为 tests/ User.php <?php class Errorcode { const NAME_IS_NULL = 0; } class ...
- 安卓下H5弹窗display:table的bug
表单以弹窗的形式弹出时,若设置了表单的div:display:table下,安卓打开页面输入法的时候,表单顶到屏幕顶部之后,再也无法上滑,键盘遮住了下面的输入框.在ios下,一切显示正常,因为iOS会 ...
- pta l2-5(集合相似度)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805070149828608 题意:求两个集合的相同的不同元素的 ...