How do I check if a string is a number (float)?

后端 未结 30 3972
暗喜
暗喜 2020-11-21 05:16

What is the best possible way to check if a string can be represented as a number in Python?

The function I currently have right now is:

def is_numb         


        
30条回答
  •  青春惊慌失措
    2020-11-21 05:56

    In case you are looking for parsing (positive, unsigned) integers instead of floats, you can use the isdigit() function for string objects.

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

    String Methods - isdigit(): Python2, Python3

    There's also something on Unicode strings, which I'm not too familiar with Unicode - Is decimal/decimal

提交回复
热议问题