How can I check if a string contains only numbers and lower case letters?
I only managed to check if it contains numbers and lower case letters and doesn't contain upper case letters but I don't know how to check that it doesn't contain any symbols as ^&*(% etc..
if(any(i.islower() for i in password) and any(i.isdigit() for i in password) and not any(i.isupper() for i in password)):
EDIT: so apparently I need to do this without using any loops and mainly using functions like .islower() , .isdigit() , isalnum() etc.. and I have no idea how I can check if a string contains lower case letters and numbers only , without using loops or something that will check every single char in the string . we only started to learn the basics in python so they told us we can't use "for" and all that even if I know what it does.. now I can check if an entire string is only digits or lower/upper case letters but I don't know how to check the two conditions mentioned above in the simplest way possible
To check whether a String contains only unicode letters or digits in Java, we use the isLetterOrDigit() method and charAt() method with decision-making statements. The isLetterOrDigit(char ch) method determines whether the specific character (Unicode ch) is either a letter or a digit.
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 string contains special characters, call the test() method on a regular expression that matches any special character. The test method will return true if the string contains at least 1 special character and false otherwise.
How about use regex:
>>> def is_digit_and_lowercase_only(s):
return re.match("^[\da-z]+$", s)
>>> print is_digit_and_lowercase_only("dA")
None
>>> print is_digit_and_lowercase_only("adc87d6f543sc")
<_sre.SRE_Match object at 0x107c46988>
It'll return None
if match failed, so you can use it with if
.
What about:
if all(c.isdigit() or c.islower() for c in password):
after all, you want to check that all characters are either a digit or lowercase letters. So for all characters c
, that character is c.isdigit() or c.islower()
. Now an all(..)
takes as input an iterable of values and checks if the truthiness of all these values is True
. So from the moment there is one digit that does not satisfies our condition, the all(..)
will return False
.
Mind however that all(..)
is True
if there are no elements. Indeed if the password
is the empty string, all the characters satisfy this condition, since there are no characters.
EDIT:
In case you want to check that the password
contains both digits and lowercase characters, you can alter the condition to:
if all(c.isdigit() or c.islower() for c in password) and \
any(c.isdigit() for c in password) and \
any(c.islower() for c in password):
Now the check will only succeed if there are at least two characters in password
: a lower and a digit.
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