python 判断字符串是否为数字

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

猜你喜欢

转载自www.cnblogs.com/sea-stream/p/12822060.html