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
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 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)
:
Now the check will only succeed if there are at least two characters in password
: a lower and a digit.
Another solution is to count the letters of each type and make sure they're not zero (in this context True equates to 1 and False to 0):
def validate_password(password):
"""
Return True if password contains digits and lowercase letters
but nothing else and is at least 8 characters long; otherwise
return False.
"""
ndigits = sum(c.isdigit() for c in password)
nlower = sum(c.islower() for c in password)
password_length = len(password)
return (password_length > 7 and ndigits and nlower and
(ndigits+nlower)==password_length)
Define a function that applies your rules using sets for membership testing .
import string
lower = set(string.ascii_lowercase)
digits = set(string.digits)
def valid(s):
'''Test string for valid characters, and composition'''
s = set(s)
invalid = s.difference(lower, digits)
both = s.intersection(lower) and s.intersection(digits)
return bool(both and not invalid)
Usage:
>>> valid('12234')
False
>>> valid('abcde')
False
>>> valid('A123')
False
>>> valid('a$1')
False
>>> valid('1a')
True
>>>
simplest answer from the top of my head:
if str1 == str1.lower():
# string is only lower case
else:
# string is not only lower case
You can use isdigit() or islower() method to check string contains only numbers and lower case letters
import string
input_str=raw_input()
for ch in input_str:
if ch.isdigit() or ch.islower():
output_str=True
else:
output_str=False
break
print output_str
In [87]: '123anydigitorletterorevenunicodeßßидажелатиница'.isalnum()
Out[87]: True
In [88]: '123anydigitorletterorevenunicodeßßидажелатиница'.islower()
Out[88]: True
So the solution is
if password.islower() and password.isalnum():
...some code...
I can not figure out what for to iterate the string
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.