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

Python Code Example: Enhanced Password Validator

This Python code example provides an advanced password validation and strength assessment tool. It features modular functions to check if a password meets minimum length requirements, count the occurrence of uppercase, lowercase, digit, and special characters, and determine the overall strength of the password. The script offers comprehensive feedback on missing character types, ensuring users are informed about how to improve their passwords for better security. This approach enhances code readability, maintainability, and efficiency, making it suitable for advanced Python developers seeking a robust password validation solution.

Code Example

def check_length(password):
    return len(password) >= 6

def count_characters(password):
    upper_chars = sum(1 for c in password if c.isupper())
    lower_chars = sum(1 for c in password if c.islower())
    digits = sum(1 for c in password if c.isdigit())
    special_chars = sum(1 for c in password if not c.isalnum())
    return upper_chars, lower_chars, digits, special_chars

def password_strength(password):
    length = len(password)
    upper_chars, lower_chars, digits, special_chars = count_characters(password)

    if not check_length(password):
        return "Password must be at least 6 characters long!"

    feedback = []

    if upper_chars == 0:
        feedback.append("Password must contain at least one uppercase character!")
    if lower_chars == 0:
        feedback.append("Password must contain at least one lowercase character!")
    if special_chars == 0:
        feedback.append("Password must contain at least one special character!")
    if digits == 0:
        feedback.append("Password must contain at least one digit!")

    if feedback:
        return "\n".join(feedback)
    
    if length >= 10:
        return "The strength of the password is strong."
    else:
        return "The strength of the password is medium."

def check_password(password):
    feedback = password_strength(password)
    print(feedback)

password = input("Please enter password: ")
check_password(password)

Output

Example 1: Weak Password (missing uppercase, special character, and digit)

Please enter password: test
Password must be at least 6 characters long!
Password must contain at least one uppercase character!
Password must contain at least one special character!
Password must contain at least one digit!

Example 2: Weak Password (missing uppercase, special character, and digit, but meets length requirement)

Please enter password: testing
Password must contain at least one uppercase character!
Password must contain at least one special character!
Password must contain at least one digit!

Example 3: Medium Strength Password

Please enter password: Test123!
The strength of the password is medium.

Example 4: Strong Password

Please enter password: Test123!Secure
The strength of the password is strong.

Example 5: Weak Password (missing uppercase)

Please enter password: test123!
Password must contain at least one uppercase character!

Code Explanation

Define the check_length function:

def check_length(password):
    return len(password) >= 6

This function takes a password as input and returns True if the password length is at least 6 characters, and False otherwise.

Define the count_characters function:

def count_characters(password):
    upper_chars = sum(1 for c in password if c.isupper())
    lower_chars = sum(1 for c in password if c.islower())
    digits = sum(1 for c in password if c.isdigit())
    special_chars = sum(1 for c in password if not c.isalnum())
    return upper_chars, lower_chars, digits, special_chars

This function counts the number of uppercase characters, lowercase characters, digits, and special characters in the password. It returns these counts as a tuple.

Define the password_strength function:

def password_strength(password):
    length = len(password)
    upper_chars, lower_chars, digits, special_chars = count_characters(password)

    if not check_length(password):
        return "Password must be at least 6 characters long!"

    feedback = []

    if upper_chars == 0:
        feedback.append("Password must contain at least one uppercase character!")
    if lower_chars == 0:
        feedback.append("Password must contain at least one lowercase character!")
    if special_chars == 0:
        feedback.append("Password must contain at least one special character!")
    if digits == 0:
        feedback.append("Password must contain at least one digit!")

    if feedback:
        return "\n".join(feedback)
    
    if length >= 10:
        return "The strength of the password is strong."
    else:
        return "The strength of the password is medium."

This function evaluates the strength of the password. It first checks the length of the password. If it’s shorter than 6 characters, it returns a message indicating that the password is too short. Then it checks if the password contains at least one uppercase character, one lowercase character, one digit, and one special character. If any of these conditions are not met, it returns the corresponding feedback messages. If all conditions are met, it further evaluates the password strength based on its length (strong if at least 10 characters, medium otherwise).

Define the check_password function:

def check_password(password):
    feedback = password_strength(password)
    print(feedback)

This function calls the password_strength function and prints the feedback.

Prompt the user for a password and check its strength:

password = input("Please enter password: ")
check_password(password)

How It Works:

  • The user is prompted to enter a password.
  • The password is checked for length and character composition (uppercase, lowercase, digits, special characters).
  • Appropriate feedback is provided based on the password’s characteristics and length.
  • The password strength is evaluated as strong or medium if it meets all character requirements and is sufficiently long.