你如何使用:isalnum,isdigit,isupper来测试字符串的每个字符? [英] How do you use: isalnum, isdigit, isupper to test each character of a string?

查看:194
本文介绍了你如何使用:isalnum,isdigit,isupper来测试字符串的每个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个密码强度模拟器,要求用户输入密码,然后给出一个分数。



我使用的是:

  islanum()
isdigit()
isupper()

试试看看输入的密码有多好。



我不希望返回布尔值,而是要评估密码的每个字符,然后程序将所有True值加起来,并将其转换为得分了。示例代码:

  def upper_case():
points = int(0)
limit = 3
每个在pword中:
如果each.isupper():
points = points + 1
返回点
else:
返回0

任何帮助都将不胜感激! THANKS !!

解决方案

.isalnum() .isupper() .isdigit()和好友是 str 输入Python,并调用像这样:

 >>> s =aBc123
>>> s [0] .isalnum()
True
>>> s [1] .isupper()
True
>>> s $ [$ c






$ b $ <$ $ $ $ c> getscore()
功能:

  s =aBc123 @!xY 

def getscore(s):
score = 0
for s in:
if c.isupper():
score + = 2
elif c.isdigit():
得分+ = 2
elif c.isalpha():
得分+ = 1
其他:
得分+ = 3
返回分数

打印getscore(s)

输出:

  13 

更好的版本:

  s =aBc123 @!xY


def getscore(s):
return len(s)+ len([c for s in c.isdigit()or c.isupper ()或不是c.isalpha()])

打印getscore(s)



输出:

  17 


I am trying to make a password strength simulator which asks the user for a password and then gives back a score.

I am using:

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() 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

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)

Output:

13

Better Version:

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)

Output:

17

这篇关于你如何使用:isalnum,isdigit,isupper来测试字符串的每个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆