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. Hosts

    Hosts "C:\Windows\System32\drivers\etc\hosts" [最新]2018 hosts 持续更新[更新于:2018-11-13] 本页面WordP ...

  2. FMS Dev Guide学习笔记(远程共享对象)

    一.开发交互式的媒体应用程序1.共享对象(Shared objects)    ----远程共享对象 在你创建一个远程共享对象之前,创建一个NetConnection对象并且连接到服务器.一旦你创建了 ...

  3. css flex 属性教程

    https://www.zhangxinxu.com/wordpress/2018/10/display-flex-css3-css/#align-self display: flex; flex-w ...

  4. linux 安装nexus3

    准备工作: 环境:linux 系统:centos6.4-x86-x64 安装工具:nexus-3.14 软件下载:nexus-3.14 官网下载地址:点击打开链接 将下载的压缩包通过xft5上传至/o ...

  5. php导出excel不知道列数 php26进制函数

    function num2Letter($num) { $num = intval($num); if ($num <= 0) return false; $letterArr = array( ...

  6. destoon手机端分页

    $pages = ''; $pagesize = 2; $offset = ($page-1)*$pagesize; $rr = $db->get_one("SELECT COUNT( ...

  7. WEB常用前端开发调试工具介绍

    只要是设计开发,就需要进行调试,尽管相对来说,前端的调试要简单一些,但使用一些调试工具或插件还是能提高你的工作效率.下面是一些主要用于IE浏览器环境和Firefox浏览器环境等的调试工具简介. 一.I ...

  8. 2、CDH 搭建Hadoop在安装(安装Cloudera Manager,CDH和托管服务)

    安装Cloudera Manager,CDH和托管服务 建议使用此过程为生产环境安装Cloudera Manager和CDH.对于非生产“易于安装”,请参阅安装概念证明群集. 在开始安装之前,请确保已 ...

  9. sql根据最小值去重

    CREATE TABLE temp2 AS SELECT MAX(id) id FROM sys_oper_procenter GROUP BY pro_title 创建一个temp2的表 根据标题分 ...

  10. leetcode 链表类型题总结

    链表测试框架示例: // leetcodeList.cpp : 定义控制台应用程序的入口点.vs2013 测试通过 // #include "stdafx.h" #include ...