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

后端 未结 30 3933
暗喜
暗喜 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:37

    You can use Unicode strings, they have a method to do just what you want:

    >>> s = u"345"
    >>> s.isnumeric()
    True
    

    Or:

    >>> s = "345"
    >>> u = unicode(s)
    >>> u.isnumeric()
    True
    

    http://www.tutorialspoint.com/python/string_isnumeric.htm

    http://docs.python.org/2/howto/unicode.html

提交回复
热议问题