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

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 循环
(追記) (追記ここまで)

Python 判断字符串是否为数字

Document 对象参考手册 Python3 实例

以下实例通过创建自定义函数 is_number() 方法来判断字符串是否为数字:

实例(Python 3.0+)

# -*- coding: UTF-8 -*-# Filename : test.py# author by : www.runoob.comdefis_number(s): try: float(s)returnTrueexceptValueError: passtry: importunicodedataunicodedata.numeric(s)returnTrueexcept(TypeError, ValueError): passreturnFalse# 测试字符串和数字print(is_number('foo'))# Falseprint(is_number('1'))# Trueprint(is_number('1.3'))# Trueprint(is_number('-1.37'))# Trueprint(is_number('1e3'))# True# 测试 Unicode# 阿拉伯语 5print(is_number('٥'))# True# 泰语 2print(is_number(''))# True# 中文数字print(is_number(''))# True# 版权号print(is_number('©'))# False

我们也可以使用内嵌 if 语句来实现:

执行以上代码输出结果为:

False
True
True
True
True
True
True
True
False

更多方法

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

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

Document 对象参考手册 Python3 实例

AI 思考中...

2 篇笔记 写笔记

  1. #0

    浮点、

    246***[email protected]

    320
    #教程代码当出现多个汉字数字时会报错,通过遍历字符串解决
    #对汉字表示的数字也可分辨
    def is_number(s):
     try: # 如果能运行float(s)语句,返回True(字符串s是浮点数)
     float(s)
     return True
     except ValueError: # ValueError为Python的一种标准异常,表示"传入无效的参数"
     pass # 如果引发了ValueError这种异常,不做任何事情(pass:不做任何事情,一般用做占位语句)
     try:
     import unicodedata # 处理ASCii码的包
     for i in s:
     unicodedata.numeric(i) # 把一个表示数字的字符串转换为浮点数返回的函数
     #return True
     return True
     except (TypeError, ValueError):
     pass
     return False

    浮点、

    246***[email protected]

    7年前 (2019年07月17日)
  2. #0

    leaf_cq

    all***[email protected]

    89

    进一步扩展到全角数字:

    # 进一步扩展到全角数字
    def is_number(s):
     try:
     float(s)
     return True
     except ValueError:
     pass
     import unicodedata
     try:
     unicodedata.numeric(s)
     return True
     except (TypeError, ValueError):
     pass
     
     if len(s) < 2:
     return False
     try:
     d = 0
     if s.startswith('-'):
     s = s[1:]
     for c in s:
     if c == '-': # 全角减号
     return False
     
     if c == '.': # 全角点号
     if d > 0:
     return False
     else:
     d = 1
     continue
     unicodedata.numeric(c)
     return True
     except (TypeError, ValueError):
     pass
     return False
    # 测试字符串和数字
    print(f'{is_number("foo")}')
    print(f'{is_number("1") }')
    print(f'{is_number("1.3") }')
    print(f'{is_number("-1.37") }')
    print(f'{is_number("1e3") }')
    print(f'{is_number("2.345.6") }')
    print(f'{is_number("-5.2-8") }')
    print(f'{is_number("52-8") }')
    print(f'{is_number("-.5") }')
    print(f'{is_number("-5.") }')
    print(f'{is_number(".5") }')
    # 测试Unicode
    # 阿拉伯语 5
    print(f'{is_number("٥") }')
    # 泰语 2
    print(f'{is_number("๒") }')
    # 中文数字
    print(f'{is_number("四") }')
    print(f'{is_number("四卅") }')
    # 全角数字
    print(f'{is_number("123") }')
    print(f'{is_number("-123") }')
    print(f'{is_number("-123") }')
    print(f'{is_number("12-3") }')
    print(f'{is_number("123-") }')
    print(f'{is_number("1.23") }')
    print(f'{is_number("1.23") }')
    print(f'{is_number(".23") }')
    print(f'{is_number("-.23") }')
    print(f'{is_number("1.23") }')
    print(f'{is_number("1.2.3") }')
    # 版权号
    print(f'{is_number("©") }')

    leaf_cq

    all***[email protected]

    6年前 (2020年08月05日)

点我分享笔记

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

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