Check if a string contains a number

后端 未结 16 1374
無奈伤痛
無奈伤痛 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.

    0 讨论(0)
  • 2020-11-22 11:51
    import string
    import random
    n = 10
    
    p = ''
    
    while (string.ascii_uppercase not in p) and (string.ascii_lowercase not in p) and (string.digits not in p):
        for _ in range(n):
            state = random.randint(0, 2)
            if state == 0:
                p = p + chr(random.randint(97, 122))
            elif state == 1:
                p = p + chr(random.randint(65, 90))
            else:
                p = p + str(random.randint(0, 9))
        break
    print(p)
    

    This code generates a sequence with size n which at least contain an uppercase, lowercase, and a digit. By using the while loop, we have guaranteed this event.

    0 讨论(0)
  • 2020-11-22 11:55

    any and ord can be combined to serve the purpose as shown below.

    >>> def hasDigits(s):
    ...     return any( 48 <= ord(char) <= 57 for char in s)
    ...
    >>> hasDigits('as1')
    True
    >>> hasDigits('as')
    False
    >>> hasDigits('as9')
    True
    >>> hasDigits('as_')
    False
    >>> hasDigits('1as')
    True
    >>>
    

    A couple of points about this implementation.

    1. any is better because it works like short circuit expression in C Language and will return result as soon as it can be determined i.e. in case of string 'a1bbbbbbc' 'b's and 'c's won't even be compared.

    2. ord is better because it provides more flexibility like check numbers only between '0' and '5' or any other range. For example if you were to write a validator for Hexadecimal representation of numbers you would want string to have alphabets in the range 'A' to 'F' only.

    0 讨论(0)
  • 2020-11-22 11:55

    Simpler way to solve is as

    s = '1dfss3sw235fsf7s'
    count = 0
    temp = list(s)
    for item in temp:
        if(item.isdigit()):
            count = count + 1
        else:
            pass
    print count
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-22 12:00

    use

    str.isalpha() 

    Ref: https://docs.python.org/2/library/stdtypes.html#str.isalpha

    Return true if all characters in the string are alphabetic and there is at least one character, false otherwise.

    0 讨论(0)
提交回复
热议问题