首页 > 编程语言 > 详细

python 判断字符串是否为数字

时间:2020-05-03 16:26:17      阅读:53      评论:0      收藏:0      [点我收藏+]

code

def is_number(s):
    try:
        float(s)
        return True
    except 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

 

 

 

 

 

 

python 判断字符串是否为数字

原文:https://www.cnblogs.com/sea-stream/p/12822060.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!