Password Strength Checker using Regular Expressions
IntermediateBuild a real-time password security analyzer with Python
1) Project Overview
The Password Strength Checker helps users create strong and secure passwords by analyzing their password's structure and providing instant feedback.
This program uses regular expressions (regex) to evaluate if a password:
- Contains uppercase and lowercase letters
- Has at least one digit
- Includes a special character
- Meets a minimum length requirement
๐ก Why it's useful: Weak passwords are a common cause of data breaches. This tool teaches users to understand password security and helps developers implement real-time validation in registration systems.
2) Learning Objectives
By completing this project, students will:
- ๐ง Understand Regular Expressions (re module) and pattern matching
- ๐งฉ Learn string validation using multiple conditions
- ๐ฌ Practice user input handling and formatted output
- ๐ Use functions for modular and reusable code
- ๐ Learn how to rate password strength dynamically (weak, medium, strong)
3) Step-by-Step Explanation
Follow these steps to build the password checker:
- Import the Required Library โ Python's built-in re (Regular Expressions) library helps us define complex validation patterns easily
- Create the Password Validation Function โ We'll define check_password_strength(password) to test the password against several regex patterns, calculate how many conditions it passes, and return a message indicating whether it's weak, medium, or strong
- Define Validation Criteria โ We'll check for:
- Minimum length (8 characters)
- At least one uppercase letter
- At least one lowercase letter
- At least one digit
- At least one special symbol (@, #, $, %, &, *, etc.)
- Use Regex Patterns โ Examples: [A-Z] โ Uppercase letters, [a-z] โ Lowercase letters, [0-9] โ Digits, [@#$%^&*()_+=!~] โ Special characters
- Display the Result โ Use conditional logic to classify: Weak โ Less than 3 conditions met, Medium โ 3โ4 conditions met, Strong โ All conditions met
4) Complete, Well-Commented, and Verified Python Code
You can copy this into a file named password_checker.py and run it.
# ---------------------------------------------------
# ๐ Password Strength Checker using Regular Expressions
# ---------------------------------------------------
# Author: Your Name
# Level: Intermediate
# Verified with Python 3.8+
# ---------------------------------------------------
import re
def check_password_strength(password):
"""Evaluates and returns the strength of a password."""
# Initialize score
score = 0
# Criteria 1: Length at least 8
if len(password) >= 8:
score += 1
# Criteria 2: Contains uppercase letter
if re.search(r"[A-Z]", password):
score += 1
# Criteria 3: Contains lowercase letter
if re.search(r"[a-z]", password):
score += 1
# Criteria 4: Contains a digit
if re.search(r"[0-9]", password):
score += 1
# Criteria 5: Contains special character
if re.search(r"[@#$%^&*()_+=!~]", password):
score += 1
# Determine strength
if score <= 2:
strength = "Weak ๐ด"
elif score == 3 or score == 4:
strength = "Medium ๐ก"
else:
strength = "Strong ๐ข"
return strength
# --------------------------
# Main Program Execution
# --------------------------
if __name__ == "__main__":
print("๐ Password Strength Checker")
print("-" * 35)
# Get password input from user
user_password = input("Enter your password: ")
# Check strength
result = check_password_strength(user_password)
# Display result
print(f"\nPassword Strength: {result}")
# Suggest improvement if weak
if "Weak" in result:
print("โ Tip: Use uppercase, lowercase, digits, and symbols to make it stronger.")
elif "Medium" in result:
print("โ ๏ธ Almost there! Add more variety (special chars or numbers).")
else:
print("โ
Excellent! Your password is strong and secure.")
โ Libraries Used: Only re (built-in).
5) Sample Output
Example 1: Weak Password
-----------------------------------
Enter your password: hello
Password Strength: Weak ๐ด
โ Tip: Use uppercase, lowercase, digits, and symbols to make it stronger.
Example 2: Medium Password
Password Strength: Medium ๐ก
โ ๏ธ Almost there! Add more variety (special chars or numbers).
Example 3: Strong Password
Password Strength: Strong ๐ข
โ Excellent! Your password is strong and secure.
6) Extension Challenge
๐ฏ Make it more advanced
Goal: Enhance your password checker with these features:
- Add a Tkinter GUI: Create a user-friendly interface with a password input box and color-coded strength indicator
- Visual Strength Bar: Use ASCII or progress bar (tqdm or tkinter.ttk.Progressbar) to visualize strength
- Password Generator Integration: Combine this with a password generator to suggest strong random passwords automatically
7) Summary
In this project, you built a real-time password strength analyzer using Python's re module.
You practiced:
- Regex pattern matching
- Input validation
- Conditional logic
- Modular function design
๐ก "Cybersecurity starts with strong passwords โ and you've just automated that with Python!"
You've now taken a real-world problem and solved it using intermediate-level programming logic that's practical, reusable, and secure.