Python isdigit() 方法检测字符串是否只由数字组成。

语法

isdigit()方法语法:

str.isdigit()

参数

无。

返回值

如果字符串只包含数字则返回 True 否则返回 False。

Python isdecimal() 方法检查字符串是否只包含十进制字符。这种方法只存在于unicode对象。

注意:定义一个十进制字符串,只需要在字符串前添加 'u' 前缀即可。

语法

isdecimal()方法语法:

str.isdecimal()

参数

返回值

如果字符串是否只包含十进制字符返回True,否则返回False。

1 str = u"this2009";2 printstr.isdecimal();3

4 str = u"23443434";5 print str.isdecimal()

输出结果:

1 False2 True

Python isnumeric() 方法检测字符串是否只由数字组成。这种方法是只针对unicode对象。

注:定义一个字符串为Unicode,只需要在字符串前添加 'u' 前缀即可。

语法

isnumeric()方法语法:

str.isnumeric()

参数

无。

返回值

如果字符串中只包含数字字符,则返回 True,否则返回 False

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 isalnum函数_Python 字符串 (isdigit, isalnum,isnumeric)转相关推荐

  1. python islower函数_python字符串是否是小写-python 字符串小写-python islower函数-python islower函数未定义-嗨客网...

    Python字符串是否是小写教程 在开发过程中,有时候我们需要判断一个 Python islower()函数详解 语法 str.islower() -> bool 参数 参数 描述 str 表示 ...

  2. python substr函数_Python 字符串函数

    1.去空格及特殊符号 s.strip() s.lstrip() s.rstrip() s.strip().lstrip().rstrip(',') 声明:s为字符串,rm为要删除的字符序列 s.str ...

  3. python解码函数_Python字符串中的两个解码与解码函数详解

    Python字符串作为计算机语言中被广泛应用的语言.很少有人知道Python字符串中有两个编码和解码的函数.以下是文章的介绍,希望你能从以下的文章中会有所收获,下面是文章的介绍. Python字符串, ...

  4. python 字符串函数_Python字符串函数

    python 字符串函数 Python provides a lot of built-in functions to manipulate strings. Python String is imm ...

  5. python字符串的内置函数_python——字符串内置函数

    当字符串本身就含有\n之类的,可以用转义符.行显示(在字符串引号前面加r).repr(str)三种方法. 字符串内置方法 字符串内置方法: 1. capitalize(...) S.capitaliz ...

  6. python中str函数_python字符串str的常用函数

    1 大小写相关的函数,将字符串改成大写upper,改成小写lower,大小写切换swapcase,首字母大写capitalize,每个单词的首字母大写title,判断是否为大写isupper,判断是否 ...

  7. python isdigit和isnumeric_python字符串isdigit、isnumeric、isdecimal区别

    isdigit().isnumeric().isdecimal()是python语言中字符串的内置类型.这三个函数主要区别是由于Unicode类型产生的. decimal字符举例: "123 ...

  8. python中去除字符串中首尾空格的函数_Python字符串的首尾空格如何去掉?

    Python程序设计中,会用到很多字符串,字符串中可能包含空格.而空格看不见摸得着,因此空格可能会带来很多意想不到的错误,那么字符串的空格如何去掉? 今天我们就来学习,利用函数去掉字符串里的空格. 空 ...

  9. python自定义函数复制字符串_Python学习笔记摘要(一)类型 字符串 函数 列表 深浅拷贝...

    python中的对象和类型 在python中,认为系统中的每一个"东西"都是一个对象,在python中,"对象"有着特殊的意义,python中的对象有: 一个标 ...

最新文章

  1. Node.js中文件上传与multer模块的应用
  2. OpenKruise v1.0:云原生应用自动化达到新的高峰
  3. JVM PrintGCDetails打印GC细节
  4. 艾欧币 不需繁琐存币不用学习使用钱包
  5. 批处理的高吞吐率和高延迟的解释
  6. excel 单元格名称 java_Java 创建、编辑、删除Excel命名区域
  7. 你见过最奇葩的人和事是什么?
  8. 好用的ASP.NET 分页类 简单好用 支持 AJAX 自定义文字
  9. python绘制三角函数图像_用python画三角函数
  10. TB6612与电机编码器
  11. 全网最全软件版本号相关内容科普
  12. 带你Dart带你Diao之类(二)
  13. OS实验-模拟实现首次/最佳/最坏适应算法的内存块分配和回收
  14. HTML--图片的插入
  15. qchart 坐标轴设置_QChart学习之QValueAxis坐标轴设置
  16. 一家化工厂的数字化三级跳 | 产研案例
  17. TSINGSEE青犀视频监控平台的多种联网方式详解
  18. win10下定时任务备份,bat脚本运行
  19. Excel字符函数(1):利用3个函数提取邮箱中的域名
  20. Apache ShardingSphere

热门文章

  1. 程序员面试算法_程序员的前20个搜索和排序算法面试问题
  2. java ee jms_在Java EE 7中自动配置JMS资源
  3. rest服务swagger_使用Swagger轻松记录您的Play Framework REST API
  4. jaxb xsd生成xml_使用JAXB和Jackson从XSD生成JSON模式
  5. java循坏_Java的坏功能是什么
  6. Spring Annotations我从没有机会使用第2部分:@ConfigurationProperties
  7. java8 默认方法_如何不使用Java 8默认方法
  8. jcmd,大约JDK 11
  9. javabeans_膨胀的JavaBeans –不要在您的API中添加“ Getters”
  10. javacv 人脸检测_使用JavaCV进行手和手指检测