[英]how to check if a string contains only lower case letters and numbers?
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.编辑:显然我需要在不使用任何循环的情况下执行此操作,主要使用像 .islower()、.isdigit()、isalnum() 等函数。我不知道如何检查字符串是否包含小写字母和数字,不使用循环或检查字符串中每个字符的东西。 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我们在 python 才开始学习基础知识,所以他们告诉我们不能使用“for”等等,即使我知道它的作用。现在我可以检查整个字符串是否只有数字或小写/大写字母但我不知道如何以最简单的方式检查上述两个条件
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
. 如果匹配失败,它将返回None
,因此您可以将其与if
一起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()
. 因此对于所有字符c
,该字符是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
. 现在, all(..)
将可迭代的值作为输入,并检查所有这些值的真实性是否为True
。 So from the moment there is one digit that does not satisfies our condition, the all(..)
will return False
. 因此,从一个数字不满足我们的条件的那一刻起, all(..)
将返回False
。
Mind however that all(..)
is True
if there are no elements . 但是请注意, 如果没有元素 ,则all(..)
为True
。 Indeed if the password
is the empty string, all the characters satisfy this condition, since there are no characters. 实际上,如果password
是空字符串,则所有字符都满足此条件,因为没有字符。
EDIT : 编辑 :
In case you want to check that the password
contains both digits and lowercase characters, you can alter the condition to: 如果要检查password
包含数字和小写字符,可以将条件更改为:
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. 现在只有password
中至少有两个字符才能成功检查:低位和数位。
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): 另一个解决方案是计算每种类型的字母并确保它们不为零(在此上下文中,True等于1,False为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 您可以使用isdigit()或islower()方法来检查字符串是否只包含数字和小写字母
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 我无法弄清楚迭代字符串的内容
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.