转载: 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

最新文章

  1. sql server 多列转多行实现方法
  2. 分数try catch
  3. FPSCalc——简单FPS观测类
  4. Java Hour1
  5. JS按位非(~)运算符与~~运算符的理解分析
  6. SSRS中加入书签功能及数据集窗口
  7. Java_类和对象(完美总结)_转载_覆盖和隐藏的区别,覆盖就不能使用了,而隐藏提供全局方法名或者全局变量名还可以使用
  8. Frontend Development
  9. Matlab编程-基本命令行语句
  10. BootStrap table 传递搜索参数
  11. .Net 社区虚拟大会”(dotnetConf)
  12. VS辅助工具
  13. xlrd doc
  14. java反射机制(1)
  15. 冒泡排序及优化(Java实现)
  16. Vue.js 2.x笔记:表单绑定(3)
  17. (3)java数据结构--有枚举 属性
  18. 利用python监测linux上的服务(简单实现服务宕掉自动发送邮件)
  19. 最近开始学习Cesium,学习学习。
  20. Java 微服务实践 - Spring Boot 系列

热门文章

  1. Netty源码分析之服务端启动过程
  2. React 组件性能优化探索实践
  3. errored out in DoExecute, couldn't PrepareToExecuteJITExpression
  4. DX系列之TreeList
  5. .NET Framework(.config)的配置文件架构
  6. .htaccess语法之RewriteCond与RewriteRule指令格式详细解释
  7. Unity3D Editor 扩展
  8. 常见http错误码解读
  9. HTML5 中的 canvas 画布(二)
  10. 3篇NeuroImage文献分析