Python Code Example: check password strength

In the following example, a password is checked for its strength. If the password contains less than 6 characters, it is invalid. If the password contains less than 10 characters it gets the password strength medium, if it contains more than 10 characters it is strong.

Furthermore, the password must contain at least one lowercase letter, one uppercase letter, one number and one special character. The output of this program is the strength of the password, or an indication of a missing character.

Code Explanation

This code checks the strength of a password based on certain criteria:

  1. Length of the password: It must be at least 6 characters long.
  2. Presence of different types of characters: The password must contain at least one uppercase character, one lowercase character, one special character, and one digit.
  3. Strength of the password: Based on the number of different types of characters present and the length of the password, the strength of the password is determined. If the password meets the criteria mentioned above and its length is 10 or more characters, it is considered a strong password. If it meets the criteria but its length is less than 10, it is considered a medium password.

The code takes the password as input from the user and calls the function checkPassword(password) to check the password strength. The function checkPassword(password) first checks if the length of the password is less than 6, and if it is, it prints a message saying that the password must be at least 6 characters long. If the length of the password is 6 or more, the function then loops through each character of the password and increments the variables upperChars, lowerChars, specialChars, and digits if the character is an uppercase letter, lowercase letter, special character, or digit respectively. Finally, the function checks if all the different types of characters are present in the password, and if they are, it checks the length of the password. Based on the length of the password, the function determines and prints the strength of the password. If any of the different types of characters are not present in the password, the function prints a message saying which type of character is missing.

def checkPassword(password):
    upperChars, lowerChars, specialChars, digits, length = 0, 0, 0, 0, 0
    length = len(password)

    if (length < 6):
        print("Password must be at least 6 characters long!\n")
    else:
        for i in range(0, length):
            if (password[i].isupper()):
                upperChars += 1
            elif (password[i].islower()):
                lowerChars += 1
            elif (password[i].isdigit()):
                digits += 1
            else:
                specialChars += 1

    if (upperChars != 0 and lowerChars != 0 and digits != 0 and specialChars != 0):
        if (length >= 10):
            print("The strength of password is strong.\n")
        else:
            print("The strength of password is medium.\n")
    else:
        if (upperChars == 0):
            print("Password must contain at least one uppercase character!\n")
        if (lowerChars == 0):
            print("Password must contain at least one lowercase character!\n")
        if (specialChars == 0):
            print("Password must contain at least one special character!\n")
        if (digits == 0):
            print("Password must contain at least one digit!\n")


password = input("Please enter password: ")
checkPassword(password)
Output
[first_run]
Please enter password: 34rT%
Password must be at least 6 characters long!

[second_run]
Please enter password: 23456weretsDFDS
Password must contain at least one special character!

[third_run]
Please enter password: 456ztr7TZ$$
The strength of password is strong.