Can someone give an idea on how to test a string that:
The islower() method returns True if all alphabets in a string are lowercase alphabets. If the string contains at least one uppercase alphabet, it returns False.
To check if a letter in a string is uppercase or lowercase use the toUpperCase() method to convert the letter to uppercase and compare it to itself. If the comparison returns true , then the letter is uppercase, otherwise it's lowercase. Copied!
Python Isupper() To check if a string is in uppercase, we can use the isupper() method. isupper() checks whether every case-based character in a string is in uppercase, and returns a True or False value depending on the outcome.
Introduction to the Python String isupper() methodThe string isupper() method returns True if all cased characters in a string are uppercase. Otherwise, it returns False . If the string doesn't contain any cased characters, the isupper() method also returns False .
if (any(x.isupper() for x in s) and any(x.islower() for x in s)
and any(x.isdigit() for x in s) and len(s) >= 7):
Another way is to express your rules as a list of (lambda) functions
rules = [lambda s: any(x.isupper() for x in s), # must have at least one uppercase
lambda s: any(x.islower() for x in s), # must have at least one lowercase
lambda s: any(x.isdigit() for x in s), # must have at least one digit
lambda s: len(s) >= 7 # must be at least 7 characters
]
if all(rule(s) for rule in rules):
...
Regarding your comment. To build an error message
errors = []
if not any(x.isupper() for x in password):
errors.append('Your password needs at least 1 capital.')
if not any(x.islower() for x in password):
errors.append(...)
...
if errors:
print " ".join(errors)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With