Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python check if a input has a digit in it?

Im trying to see how i can see if a number is in a users input. i tried using .isdigit() but only works if its just a number. im trying to add it to a password checker. i also tried .isalpha() but didn't work. what have i done wrong and what do i need to add or change?

here is what i have

   password = input('Please type a password ')
   str = password
   if str.isdigit() == True:

    print('password has a number and letters!')
    else:
            print('You must include a number!')`
like image 562
theRumblers100 Avatar asked Apr 18 '26 02:04

theRumblers100


1 Answers

You can use a generator expression and an isdigit() within the any function :

if any(i.isdigit() for i in password) :
       #do stuff

The advantage of using any is that it doesn't traverse the whole string and will return a bool value if it finds a digit for the first time!

It is equal to the fallowing function :

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
like image 198
Mazdak Avatar answered Apr 20 '26 15:04

Mazdak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!