首页 文章

如何检查字符串是否只包含小写字母和数字?

提问于
浏览
2

如何检查字符串是否包含 only 数字和小写字母?

我只设法检查它是否包含数字和小写字母,并且不包含大写字母但我不知道如何检查它不包含任何符号为^&*(%等等..

if(any(i.islower() for i in password) and any(i.isdigit() for i in password) and not any(i.isupper() for i in password)):

编辑:显然我需要这样做而不使用任何循环,主要使用.islower(),. isdigit(),isalnum()等函数 . 我不知道如何检查字符串是否包含小写字母和数字,不使用循环或将检查字符串中每个字符的东西 . 我们只是开始学习python中的基础知识,所以他们告诉我们我们不能使用“for”和所有这些,即使我知道它的作用..现在我可以检查整个字符串是否只是数字或小写/大写字母但我不知道如何以最简单的方式检查上述两个条件

6 回答

  • 4

    关于什么:

    if all(c.isdigit() or c.islower() for c in password):
    

    毕竟,您要检查 all 字符是数字还是小写字母 . 因此,对于所有字符 c ,该字符为 c.isdigit() or c.islower() . 现在 all(..) 将可迭代的值作为输入,并检查所有这些值的真实性是否为 True . 因此,从一个数字不满足我们的条件的那一刻起, all(..) 将返回 False .

    但请注意 all(..) is True if there are no elements . 实际上如果 password 是空字符串, all 字符满足这个条件,因为没有字符 .

    EDIT

    如果要检查 password 是否包含数字和小写字符,可以将条件更改为:

    if all(c.isdigit() or c.islower() for c in password) and \
           any(c.isdigit() for c in password) and \
           any(c.islower() for c in password):
    

    现在检查只有在 password 中至少有两个字符时才会成功:低位和数位 .

  • 0

    如何使用regex

    >>> def is_digit_and_lowercase_only(s):
            return re.match("^[\da-z]+$", s)
    >>> print is_digit_and_lowercase_only("dA")
    None
    >>> print is_digit_and_lowercase_only("adc87d6f543sc")
    <_sre.SRE_Match object at 0x107c46988>
    

    如果匹配失败,它将返回 None ,因此您可以将其与 if 一起使用 .

  • 1

    另一个解决方案是计算每种类型的字母并确保它们不为零(在此上下文中,True等于1,False为0):

    def validate_password(password):
        """
        Return True if password contains digits and lowercase letters
        but nothing else and is at least 8 characters long; otherwise
        return False.
    
        """
    
        ndigits = sum(c.isdigit() for c in password)
        nlower = sum(c.islower() for c in password)
        password_length = len(password)
        return (password_length > 7 and ndigits and nlower and
                (ndigits+nlower)==password_length)
    
  • 1

    使用集合定义一个应用规则的函数进行成员资格测试 .

    import string
    lower = set(string.ascii_lowercase)
    digits = set(string.digits)
    def valid(s):
        '''Test string for valid characters, and composition'''
    
        s = set(s)
        invalid = s.difference(lower, digits)
        both = s.intersection(lower) and s.intersection(digits)
        return bool(both and not invalid)
    

    用法:

    >>> valid('12234')
    False
    >>> valid('abcde')
    False
    >>> valid('A123')
    False
    >>> valid('a$1')
    False
    >>> valid('1a')
    True
    >>>
    
  • 0

    您可以使用isdigit()或islower()方法来检查字符串是否只包含数字和小写字母

    import string
    input_str=raw_input()
    
    
    for ch in input_str:
    
        if  ch.isdigit() or  ch.islower():
            output_str=True
    
        else:
            output_str=False
            break   
    
    print output_str
    
  • 1
    In [87]: '123anydigitorletterorevenunicodeßßидажелатиница'.isalnum()
    Out[87]: True
    
    In [88]: '123anydigitorletterorevenunicodeßßидажелатиница'.islower()
    Out[88]: True
    

    所以解决方案是

    if password.islower() and password.isalnum():
        ...some code...
    

    我无法弄清楚迭代字符串的内容

相关问题