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)转的更多相关文章

  1. Python中判断字符串是否为数字的三个方法isdecimal 、isdigit、isnumeric的差别

    isdecimal .isdigit.isnumeric这三个字符串方法都用于判断字符串是否为数字,为什么用三个方法呢?他们的差别是什么内? isdecimal:是否为十进制数字符,包括Unicode ...

  2. Python3中isdigit(), isdecimal(), isnumeric()的区别和字符串的常用方法

    # 全部小写 string.lower() # 全部大写 string.upper() # 是否全部小写 string.islower() # 是否全部大写 string.isupper() # 首字 ...

  3. Python 字符串

    Python访问字符串中的值 Python不支持单字符类型,单字符也在Python也是作为一个字符串使用. Python访问子字符串,可以使用方括号来截取字符串,如下实例: #!/usr/bin/py ...

  4. Python 字符串处理大全.

    Python 字符串 字符串是Pyhton中常用的数据类型,我们可以使用引号来创建字符串 . 创建字符串很简单 , 就不说了 . Python 访问字符串中的值 鬼叔本着简洁 使用的设计目的 , 在设 ...

  5. python 字符串探讨

    本文内容基于python3 几乎所有有用的程序都会涉及到某些文本处理,不管是解析数据还是产生输出.字符串的学习是重点中的重点,这一节将重点关注文本的操作处理,比如提取字符串,搜索,替换以及解析等.大部 ...

  6. python字符串常用内置方法

    python字符串常用内置方法 定义: 字符串是一个有序的字符的集合,用与存储和表示基本的文本信息. python中引号中间包含的就是字符串. # s1='hello world' # s2=&quo ...

  7. python字符串内置方法

    网上已经有很多,自己操作一遍,加深印象. dir dir会返回一个内置方法与属性列表,用字符串'a,b,cdefg'测试一下 dir('a,b,cdefg') 得到一个列表 ['__add__', ' ...

  8. python字符串处理内置方法一览表

    python字符串处理内置方法一览表   序号 方法及描述 1 capitalize()将字符串的第一个字符转换为大写 2 center(width, fillchar) 返回一个指定的宽度 widt ...

  9. python字符串(string)方法整理

    python中字符串对象提供了很多方法来操作字符串,功能相当丰富. print(dir(str)) [..........'capitalize', 'casefold', 'center', 'co ...

随机推荐

  1. 自定义 mapper的实现

    json格式,要想好看直接百度,json,将字符放进去就可 一步:将mapper复制一份,名字加一个Custom自定义 二步:mpper.xml也是一样,设置里面的namespace映射关系 自定义m ...

  2. ArcGIS案例学习笔记3_1_ArcMap编辑练习

    ArcGIS案例学习笔记3_1_ArcMap编辑练习 计划时间:第三天上午 目的:ArcMap编辑练习 教程: pdf page67 数据: gis_ex10/ex07 方法: 1.新建shp文件 目 ...

  3. 360sdk网游支付服务

    网游支付服务   目录 1.流程介绍2.接口介绍2.1支付接口[客户端调用](必接)2.2支付结果通知接口–应用服务器提供接口, 由360服务器回调(必接)2.3订单核实接口– 服务器端接口, 应用服 ...

  4. 有关于tomcat启动时,利用listener来执行某个方法

    今天,项目经理让我调查一下(目的是锻炼我),刚开始的时候说用listener来实现服务器启动然后某个项目跟着启动.其实就是tomcat启动的时候去执行某个方法,通过这个方法启动某个项目.我网上调查了一 ...

  5. 对于“2017面向对象程序设计(JAVA)第四周学习总结”存在问题的反馈

    对于“2017面向对象程序设计(JAVA)第四周学习总结”存在问题的反馈 “这部分同学博文总结没有写,实验作业没有提交.”——1.关于博文作业.实验作业教学功能的正解:学习知识.暴露问题.衔接课上.2 ...

  6. Mat取行或列

    Mat dst; dst = FeatureValue.colRange(j,j+).clone(); 直接使用Mat类中成员函数,方法,colRange对应的是列,rowRange对应的是行,从第j ...

  7. webstocket 聊天

    /** * 初始化socket **/ function initSocket(index_host){//端口号 if( !window.WebSocket ){ console.log(" ...

  8. Python 图示集绵

    http://nbviewer.jupyter.org/github/pyecharts/pyecharts-users-cases/blob/master/notebook-users-cases/ ...

  9. 手机不弹toast解决方法

    经常遇到华为手机不弹toast的问题     华为手机--设置--通知栏和状态栏--通知中心--自己的项目  用户可能允许通知关了 就收不到提示了     在手机的设置 -> (某些手机前面可能 ...

  10. Push API

    [Push API] The Push API gives web applications the ability to receive messages pushed to them from a ...