Python 判断字符串是否为数字
2024-10-18 23:31:09
转载: http://www.runoob.com/python3/python3-check-is-number.html
以下实例通过创建自定义函数 is_number() 方法来判断字符串是否为数字:
# -*- 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('')) # True
print(is_number('1.3')) # True
print(is_number('-1.37')) # True
print(is_number('1e3')) # True # 测试 Unicode
# 阿拉伯语 5
print(is_number('٥')) # False
# 泰语 2
print(is_number('๒')) # False
# 中文数字
print(is_number('四')) # False
# 版权号
print(is_number('©')) # False
执行以上代码输出结果为:
False
True
True
True
True
False
False
False
False
最新文章
- sql server 多列转多行实现方法
- 分数try catch
- FPSCalc——简单FPS观测类
- Java Hour1
- JS按位非(~)运算符与~~运算符的理解分析
- SSRS中加入书签功能及数据集窗口
- Java_类和对象(完美总结)_转载_覆盖和隐藏的区别,覆盖就不能使用了,而隐藏提供全局方法名或者全局变量名还可以使用
- Frontend Development
- Matlab编程-基本命令行语句
- BootStrap table 传递搜索参数
- .Net 社区虚拟大会”(dotnetConf)
- VS辅助工具
- xlrd doc
- java反射机制(1)
- 冒泡排序及优化(Java实现)
- Vue.js 2.x笔记:表单绑定(3)
- (3)java数据结构--有枚举 属性
- 利用python监测linux上的服务(简单实现服务宕掉自动发送邮件)
- 最近开始学习Cesium,学习学习。
- Java 微服务实践 - Spring Boot 系列