In python, how do you check if a string has both uppercase and lowercase letters

后端 未结 4 664
独厮守ぢ
独厮守ぢ 2021-01-14 17:34

I\'ve looked at the other post similar to my question, Password check- Python 3, except my question involves checking if the password contains both uppercase and lower case

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-14 18:24

    You could use regular expression :

    i.e. :

    def Valid_mixed_password(password):
        lower = re.compile(r'.*[a-z]+') # Compile to match lowercase
        upper = re.compile(r'.*[A-Z]+') # Compile to match uppercase
        if lower.match(password) and upper.match(password): # if the password contains both (lowercase and uppercase letters) then return True
            return True
        else: # Else, the password does not contains uppercase and lowercase letters then return False
            return False
    
    >>> print Valid_mixed_password("hello")
    False
    >>> print Valid_mixed_password("HELLO")
    False
    >>> print Valid_mixed_password("HellO")
    True
    >>> print Valid_mixed_password("heLLo")
    True
    

    Hope it helps.

提交回复
热议问题