首页 > 解决方案 > 在一个字符串中,如果至少存在一个小写、大写、数字和特殊字符,它应该打印 true 或者 false

问题描述

def solution(a):
    s1=a.strip()
    for i in s1:
        k=i.islower()
        s=i.isupper()
        l=i.isnumeric()
        if k == True and s == True and l == True:
            print('True')

        elif l != True and s!= True and k!=True:
             print('False')

a="Hp1"
solution(a)

现在上面的函数包含检查大写,小写和数字。但是在上面运行脚本时,我没有得到任何输出。请帮助并提前谢谢。

标签: pythonstring

解决方案


这是一个使用mapany和的示例all

a="Hp1"
all((any(map(str.upper,a)), any(map(str.lower, a)), any(map(str.isnumeric, a))))
Out[21]: True

如果你想检查特殊情况,你也可以这样做

import string
string.punctuation
Out[22]: '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

包括标点符号:

a="Hp1"
all((any(map(str.upper,a)), 
     any(map(str.lower, a)), 
     any(map(str.isnumeric, a)), 
     any(map(lambda x: x in string.punctuation, a)
    )))
    
Out[23]: False

推荐阅读