Data types and variables
Operators
Modules and Packages
Conversion Programs
More Code Examples
Cheat Sheet

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 Example

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.

Code Explanation

Function Definition

def checkPassword(password):

This line defines a function named checkPassword that takes a single parameter password.

Initializing Counters

upperChars, lowerChars, specialChars, digits, length = 0, 0, 0, 0, 0
length = len(password)
  • upperChars: Counter for uppercase characters in the password.
  • lowerChars: Counter for lowercase characters in the password.
  • specialChars: Counter for special characters in the password.
  • digits: Counter for numeric digits in the password.
  • length: Stores the length of the password using the len() function.

Checking Minimum Length

if (length < 6):
    print("Password must be at least 6 characters long!\n")

This checks if the password is shorter than 6 characters. If it is, it prints a message indicating that the password must be at least 6 characters long and exits the function.

Iterating Through Password Characters

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
  • This loop iterates over each character in the password.
  • password[i].isupper(): Checks if the character is an uppercase letter.
  • password[i].islower(): Checks if the character is a lowercase letter.
  • password[i].isdigit(): Checks if the character is a digit.
  • If the character is none of the above, it is considered a special character.
  • The respective counters are incremented based on the character type.

Evaluating Password Strength

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")
  • This checks if the password contains at least one uppercase letter, one lowercase letter, one digit, and one special character.
  • If all these conditions are met:
    • If the password length is 10 or more characters, it is considered “strong”.
    • Otherwise, it is considered “medium”.

Providing Feedback for Missing Requirements

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")
  • If the password does not meet the requirement of having at least one of each character type, this block provides specific feedback about what is missing:
    • If there are no uppercase characters, it prints a corresponding message.
    • If there are no lowercase characters, it prints a corresponding message.
    • If there are no special characters, it prints a corresponding message.
    • If there are no digits, it prints a corresponding message.

Input and Function Call

password = input("Please enter password: ")
checkPassword(password)
  • input("Please enter password: "): Prompts the user to enter a password.
  • checkPassword(password): Calls the checkPassword function with the user’s input.

Summary

This code defines a function to evaluate the strength of a password based on its length and the presence of various character types. It gives detailed feedback about any missing requirements and categorizes the password’s strength as either “strong” or “medium” based on its length and complexity.