Python判断是否为数字

判断一个变量是否数字(整数、浮点数)

>>> isinstance(1, (int, long, float))
True
>>> isinstance('a', (int, long, float))
False

判断一个字符串的内容是否表示数字(整数、浮点数)

>>> foo = '123.456'
>>> foo.replace('.', '', 1).isdigit()
True
>>> bar = '12.34.56'
>>> bar.replace('.', '', 1).isdigit()
False
上一篇 Python使用POP3读取邮箱中的邮件,含文本及附件
下一篇 Python之dict(或对象)与json之间的互相转化
目录
文章列表
1 Nacos实现Spring Cloud Gateway的动态路由
Nacos实现Spring Cloud Gateway的动态路由
2
Prometheus + Grafana 使用 elasticsearch_exporter 监控 ElasticSearch
Prometheus + Grafana 使用 elasticsearch_exporter 监控 ElasticSearch
3
Spring Cloud手动定义Feign客户端
Spring Cloud手动定义Feign客户端
4
Google Guava工具类的介绍和使用
Google Guava工具类的介绍和使用
5
Kubernetes公布应用程序
Kubernetes公布应用程序
最新评论
一位WordPress评论者
一位WordPress评论者
2月12日
您好,这是一条评论。若需要审核、编辑或删除评论,请访问仪表盘的评论界面。评论者头像来自 Gravatar。