菜鸟教程 -- 学的不仅是技术,更是梦想!

Python 3 教程
Python3 教程 Python3 简介 Python3 环境搭建 Python3 VScode Python3 基础语法 Python3 基本数据类型 Python3 数据类型转换 Python3 解释器 Python3 注释 Python3 运算符 Python3 数字(Number) Python3 字符串 Python3 列表 Python3 元组 Python3 字典 Python3 集合 Python3 条件控制 Python3 循环语句 Python3 编程第一步 Python3 推导式 Python3 迭代器与生成器 Python3 with Python3 函数 Python3 lambda Python3 装饰器 Python3 数据结构 Python3 模块 Python __name__ Python3 输入和输出 Python3 File Python3 OS Python3 错误和异常 Python3 面向对象 Python3 命名空间/作用域 Python 虚拟环境的创建 Python 类型注解 Python3 标准库概览 Python3 实例 Python 测验

Python3 高级教程

Python3 正则表达式 Python3 CGI编程 Python3 MySQL(mysql-connector) Python3 MySQL(PyMySQL) Python3 网络编程 Python3 SMTP发送邮件 Python3 多线程 Python3 XML 解析 Python3 JSON Python3 日期和时间 Python3 内置函数 Python3 MongoDB Python3 urllib Python uWSGI 安装配置 Python3 pip Python3 operator Python math Python requests Python random Python OpenAI Python 有用的资源 Python AI 绘画 Python statistics Python hashlib Python 量化 Python pyecharts Python selenium 库 Python 爬虫 Python Scrapy 库 Python Markdown Python sys 模块 Python Pickle 模块 Python subprocess 模块 Python queue 模块 Python StringIO 模块 Python logging 模块 Python datetime 模块 Python re 模块 Python csv 模块 Python threading 模块 Python asyncio 模块 Python PyQt Python for 循环 Python while 循环
(追記) (追記ここまで)

Python3 isnumeric()方法

Python3 字符串 Python3 字符串


描述

isnumeric() 方法检测字符串是否只由数字组成,数字可以是: Unicode 数字,全角数字(双字节),罗马数字,汉字数字。

指数类似 2 与分数类似 1⁄2 也属于数字。

# s = '½'
s = '\u00BD'

语法

isnumeric()方法语法:

str.isnumeric()

参数

  • 无。

返回值

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

实例

以下实例展示了 isnumeric() 方法的实例:

实例

#!/usr/bin/python3

str = "runoob2016"
print (str.isnumeric())

str = "23443434"
print (str.isnumeric())

以上实例输出结果如下:

False
True

Unicode 数字:

实例

#!/usr/bin/python3

#s = '23455'
s = '\u00B23455'
print(s.isnumeric())
# s = '1⁄2'
s = '\u00BD'
print(s.isnumeric())

a = "\u0030" #unicode for 0
print(a.isnumeric())

b = "\u00B2" #unicode for 2
print(b.isnumeric())

c = "10km2"
print(c.isnumeric())

以上实例输出结果如下:

True
True
True
True
False

Python3 字符串 Python3 字符串

AI 思考中...

2 篇笔记 写笔记

  1. #0

    str.isdecimal () 与str.isdigit()的区别

    str.isdecimal() 函数只对十进制数返回 True,同时函数 str.isdigit() 对其他 unicode 支持的字符返回 True。

    详细的内容可以使用以下代码输出:

    import itertools
    line = '-' * 37
    print(line)
    print("| No | isdigit | isdecimal | chr")
    print(line)
    for number in itertools.chain(range(1000), range(4969, 4978), range(8304, 11000)):
     char = chr(number)
     if (char.isdigit() or char.isdecimal()):
     print('| {0:>6} | {1:^7} | {2:^9} | {3:3} '.format(
     number,
     '+' if char.isdigit() else '-',
     '+' if char.isdecimal() else '-',
     char
     )
     )
    9年前 (2017年10月26日)
  2. #0

    yao_yaofu

    522***[email protected]

    119

    s.isdigit、isdecimal 和 s.isnumeric 区别

    isdigit()

    True: Unicode数字,byte数字(单字节),全角数字(双字节)

    False: 汉字数字,罗马数字,小数

    Error: 无

    isdecimal()

    True: Unicode数字,,全角数字(双字节)

    False: 罗马数字,汉字数字,小数

    Error: byte数字(单字节)

    isnumeric()

    True: Unicode 数字,全角数字(双字节),汉字数字

    False: 小数,罗马数字

    Error: byte数字(单字节)

    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() # False
    num.isdecimal() # False
    num.isnumeric() # False
    num = "四" # 汉字
    num.isdigit() # False
    num.isdecimal() # False
    num.isnumeric() # True
    yao_yaofu

    yao_yaofu

    522***[email protected]

    8年前 (2018年03月06日)

点我分享笔记

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址

AltStyle によって変換されたページ (->オリジナル) /