Validation of a Password - Python

前端 未结 11 1980
自闭症患者
自闭症患者 2020-11-27 08:20

So I have to create code that validate whether a password:

  • Is at least 8 characters long
  • Contains at least 1 number
  • Contains at least 1
11条回答
  •  一整个雨季
    2020-11-27 08:34

    • isdigit() checks the whole string is a digit, not if the string contains a digit

      Return true if all characters in the string are digits and there is at least one character, false otherwise.

    • isupper() checks the whole string is in uppercase, not if the string contains at least one uppercase character.

      Return true if all cased characters in the string are uppercase and there is at least one cased character, false otherwise.

    What you need is using the any built-in function:

    • any([x.isdigit() for x in password]) will return True if at least one digit is present in password
    • any([x.isupper() for x in password]) will return True if at least one character is considered as uppercase.

提交回复
热议问题