How do you use: isalnum, isdigit, isupper to test each character of a string?(你如何使用:isalnum、isdigit、isupper 来测试字符串的每个字符?)
问题描述
我正在尝试制作一个密码强度模拟器,它要求用户输入密码,然后返回分数.
I am trying to make a password strength simulator which asks the user for a password and then gives back a score.
我正在使用:
islanum()
isdigit()
isupper()
试试看输入的密码有多好.
to try and see how good the inputted password is.
我希望它不是返回布尔值,而是评估密码的每个字符,然后程序将所有真"值相加并将其转换为分数.示例代码:
Instead of returning boolean values, I want this to assess each characters of the password, and then the program to add up all the "True" values and turn it into a score. EXAMPLE CODE:
def upper_case():
points = int(0)
limit = 3
for each in pword:
if each.isupper():
points = points + 1
return points
else:
return 0
任何帮助将不胜感激!谢谢!!
Any help would be much appreciated!! THANKS!!
推荐答案
.isalnum(), .isupper(), .isdigit() 和朋友是 Python 中 str 类型的方法,调用方式如下:
.isalnum(), .isupper(), .isdigit() and friends are methods of the str type in Python and are called like this:
>>> s = "aBc123"
>>> s[0].isalnum()
True
>>> s[1].isupper()
True
>>> s[3].isdigit()
True
简单的getscore()功能:
Simple getscore() Function:
s = "aBc123@!xY"
def getscore(s):
score = 0
for c in s:
if c.isupper():
score += 2
elif c.isdigit():
score += 2
elif c.isalpha():
score += 1
else:
score += 3
return score
print getscore(s)
输出:
13
更好的版本:
s = "aBc123@!xY"
def getscore(s):
return len(s) + len([c for c in s if c.isdigit() or c.isupper() or not c.isalpha()])
print getscore(s)
输出:
17
这篇关于你如何使用:isalnum、isdigit、isupper 来测试字符串的每个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:你如何使用:isalnum、isdigit、isupper 来测试字符串的每个字符?
基础教程推荐
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
