Skip to content

Latest commit

 

History

History
42 lines (30 loc) · 903 Bytes

74.md

File metadata and controls

42 lines (30 loc) · 903 Bytes
rank vote view answer url
74 1636 1393338 33 url

检查一个字符串是否是一个数字

如果一个字符串可以被看做一个数字那么有什么好的方法可以检测出来?

我能想到的方法:

def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        return False

我还没有想到什么更好的方法.


对字符串对象用isdigit()方法:

>>> a = "03523"
>>> a.isdigit()
True
>>> b = "963spam"
>>> b.isdigit()
False

String Methods - isdigit()

同样也有unicode的方法,但是我不太熟悉Unicode - Is decimal/decimal