Check if a string contains a number

后端 未结 16 1365
無奈伤痛
無奈伤痛 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:56

    You can use a combination of any and str.isdigit:

    def num_there(s):
        return any(i.isdigit() for i in s)
    

    The function will return True if a digit exists in the string, otherwise False.

    Demo:

    >>> king = 'I shall have 3 cakes'
    >>> num_there(king)
    True
    >>> servant = 'I do not have any cakes'
    >>> num_there(servant)
    False
    

提交回复
热议问题