Check if a string contains a number

后端 未结 16 1511
無奈伤痛
無奈伤痛 2020-11-22 11:14

Most of the questions I\'ve found are biased on the fact they\'re looking for letters in their numbers, whereas I\'m looking for numbers in what I\'d like to be a numberless

16条回答
  •  误落风尘
    2020-11-22 11:51

    I'll make the @zyxue answer a bit more explicit:

    RE_D = re.compile('\d')
    
    def has_digits(string):
        res = RE_D.search(string)
        return res is not None
    
    has_digits('asdf1')
    Out: True
    
    has_digits('asdf')
    Out: False
    

    which is the solution with the fastest benchmark from the solutions that @zyxue proposed on the answer.

提交回复
热议问题