Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check if a string contains only lower case letters and numbers?

Tags:

python

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

like image 583
shiftknite Avatar asked Apr 01 '17 14:04

shiftknite


People also ask

How do you check if a string only has letters and numbers?

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.

How do you know if a string has lower case letters?

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.

How do you check if a string has special characters or numbers?

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.


2 Answers

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.

like image 102
shizhz Avatar answered Oct 09 '22 21:10

shizhz


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.

like image 43
Willem Van Onsem Avatar answered Oct 09 '22 21:10

Willem Van Onsem