python - 在一个字符串中,如果至少存在一个小写、大写、数字和特殊字符,它应该打印 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)
现在上面的函数包含检查大写,小写和数字。但是在上面运行脚本时,我没有得到任何输出。请帮助并提前谢谢。
解决方案
这是一个使用map
、any
和的示例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
推荐阅读
- nginx - 使用包含指令时,此处不允许使用 Nginx 错误位置指令
- javascript - 与 React 中的另一个数组相比,检查和显示数组的值
- r - 在ggplot2中将legend.text显示为整数而不是十进制数
- python - 无法在 Django 中解决 NoReverseMatch at / 错误
- excel - 带字母和数字的自动增量?
- python - 导入包含类定义的 Python 模块时如何提供参数?
- python - 在python中拟合多元curve_fit(逻辑函数)
- sympy - sympy.Symbol 参数的约束
- go - 如何修复 go build 错误“reflect.typedmemclr not defined”?
- python - 当我尝试使用 pytube 仅下载 Youtube 视频的音频时,为什么我一直收到错误消息?