Python 如何检查字符串是否包含小写字母、大写字母和数字

Python 如何检查字符串是否包含小写字母、大写字母和数字,python,Python,我需要做一个简单的密码检查,它需要检查字符串是否至少有一个小写字母、一个大写字母和一个数字 这是我的 passwd = raw_input("enter your password: ") upper = 0 lower = 0 number = 0 while len(passwd) < 7 and upper <= 0 and lower <= 0 and number <= 0: print 'start' for x in passwd:

我需要做一个简单的密码检查,它需要检查字符串是否至少有一个小写字母、一个大写字母和一个数字

这是我的

passwd = raw_input("enter your password: ")
upper = 0
lower = 0 
number = 0
while len(passwd) < 7 and upper <= 0 and lower <= 0 and number <= 0:
    print 'start'
    for x in passwd:
        if x.isupper()==True:
            print 'upper'
            upper+=1
        elif x.islower()==True:
            print 'lower'
            lower+=1
        elif x.isalpha()==False:
            print 'num'
            number+=1
    print ('password not complex enough')
    upper = 0
    lower = 0
    number = 0
    passwd = raw_input('please enter password again ')

为什么只检查长度条件?我做错了什么?

您的检查逻辑有一些问题。首先,在<代码>之前重新设置控制变量,而<代码>条件可以检查它们。此外,您还使用了
来测试所有条件是否为真,而
是您应该使用的。如果要保持代码的基本结构,最好的方法是在while语句中只使用
True
,然后在满足所有条件后
break
循环。当然,如其他答案所示,有更简洁的方法来检查所有条件是否满足

passwd = raw_input("enter your password: ")
upper = 0
lower = 0 
number = 0
while True:
    for x in passwd:
        if x.isupper()==True:
            print 'upper'
            upper+=1
        elif x.islower()==True:
            print 'lower'
            lower+=1
        elif x.isalpha()==False:
            print 'num'
            number+=1
    if len(passwd) < 7 or upper <= 0 or lower <= 0 or number <= 0:
        print ('password not complex enough')
        passwd = raw_input('please enter password again ')
        upper = 0
        lower = 0
        number = 0
    else:
        break



print 'final password:', passwd

如果密码必须仅由ASCII字母和数字组成,则最有效的方法是使用:


您可以使用
unicodedata.category
收集字符的类别,然后检查是否存在所有预期的字符,从而简化整个逻辑,例如:

import unicodedata

is_valid = (
    len(password) >= 7 and 
    {'Ll', 'Lu', 'Nd'}.issubset(unicodedata.category(ch) for ch in password)
)

这可能对您有所帮助。我使用
sum(如果c.islower(),则字符串中c的值为1)
来查找小写字母的总数。和查找大写和数字总数的相同方法。
len(string)
查找字符串长度

string=input()
if ((sum(1 for c in string if c.islower())>=1) and (sum(1 for c in string if 
c.isupper())>=1) and (sum(1 for c in string if c.isdigit())>=1) and (len(string)>=7)):
    print("Password is complex")
else:
    print ("Password not complex enough")

“为什么它只是检查长度条件?”-你怎么会认为它只是这么做的?您在输出中获得了
下限
上限
num
——如果不是这些条件,您认为这是从哪里来的?你只是得到了一个不够复杂的
密码,因为你每次都会输出这个密码,这与你的实际条件检查结果完全无关。将
elif
更改为
if
你的
条件,而
循环仅在第一个循环中是
True
,因为一旦,更低的或数字得到
<0
它将计算为
False
,因为您使用的是
@CBroe我不认为它只是这样做,我的意思是它只使用该条件离开循环,如果字符串长度大于7,那么它将离开循环而不检查是否有大写或小写字母。当然,因为您将
len(passwd)<7
作为决定它是否进入循环体的条件之一……谢谢!我知道有更好的方法和更有效的方法可以做到这一点,我一直在考虑使用Regex,但我想让它尽可能简单易读,我认为这是最好的方法,非常好。虽然我更喜欢
{'Ll',Lu',Nd'}@Stefan使用
.issubset
来允许它接受任何iterable,并避免显式创建
集和gen comp,而不是
映射
,以保持Py2和Py3中的功能相同。。。(避免不必要的
列表
内置Py 2)。。。但是是的。。。它们都会工作的……嗯,好吧,虽然一个只要密码的列表可能没有那么浪费:-@Stefan一个非常公平的观点:)我想用最简单、最易读的方式来做,但这对我来说似乎是正确的。我在考虑使用正则表达式使它更简单,但你的方法太棒了。为什么要投入?答案有问题吗?
from string import ascii_lowercase, ascii_uppercase, digits

def is_valid(password):
    if len(password) < 7:
        return False
    password_set = set(password)
    return not any(password_set.isdisjoint(x)
                   for x in (ascii_lowercase, ascii_uppercase, digits))
 def is_valid(password):
     return (len(password) >= 7 and
             any(c.islower() for c in password) and
             any(c.isupper() for c in password) and
             any(c.isdigit() for c in password))
import unicodedata

is_valid = (
    len(password) >= 7 and 
    {'Ll', 'Lu', 'Nd'}.issubset(unicodedata.category(ch) for ch in password)
)
string=input()
if ((sum(1 for c in string if c.islower())>=1) and (sum(1 for c in string if 
c.isupper())>=1) and (sum(1 for c in string if c.isdigit())>=1) and (len(string)>=7)):
    print("Password is complex")
else:
    print ("Password not complex enough")