您现在的位置是:课程教程文章
python判断字符串是否为数字
2023-12-18 22:59课程教程文章 人已围观
微信支付v3版微信小程序支付PYTHON版
微信支付v3版微信小程序支付PYTHON版讲解微信小程序支付V3。购课提供源程序。 购课咨询请联系,微信号: dahaias...
山东省信息技术课本 Python 课程(2020版)
山东省信息技术课本 Python 课程(2020版)课程介绍 山东省信息技术教材在 2018 年泰山版中新增加了 Python 编程,探究如何...
【四二学堂】Python入门
【四二学堂】Python入门...
千锋python基础教程:python语言基础
千锋python基础教程:python语言基础课程简介: 通过本章的学习,对Python有一定的了解,掌握Python语法,可以使用...

以下实例通过创建自定义函数 is_number() 方法来判断字符串是否为数字:
更多相关内容,请参考这篇文章:《python判断字符是否为字母和数字》
# -*- coding: UTF-8 -*-
# Filename : test.py
# author by : www.runoob.com
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
# 测试字符串和数字
print(is_number('foo')) # False
print(is_number('1')) # True
print(is_number('1.3')) # True
print(is_number('-1.37')) # True
print(is_number('1e3')) # True
# 测试 Unicode
# 阿拉伯语 5
print(is_number('٥')) # True
# 泰语 2
print(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对象。
课程教程:python判断字符串是否为数字上一篇:python如何求一个数的平方
下一篇:没有了