Validate Password Strength in Python

Check if a password is strong based on length and character type rules.

IntermediateConditional ProgramsExample 19 of 20
validate-password-strength.py
1# Program to validate password strength
2
3import re
4
5password = input("Enter a password: ")
6
7length_ok = len(password) >= 8
8has_upper = re.search(r"[A-Z]", password) is not None
9has_lower = re.search(r"[a-z]", password) is not None
10has_digit = re.search(r"[0-9]", password) is not None
11has_special = re.search(r"[^A-Za-z0-9]", password) is not None
12
13if length_ok and has_upper and has_lower and has_digit and has_special:
14 print("Strong password")
15else:
16 print("Weak password")

Output

Enter a password: Abc@1234
Strong password

What's going on

Abc@1234 passes: length 8, upper, lower, digit, symbol. abc123 fails. The regexes are just "is this character class in the string". Skip the length check and "A@1" looks strong.

After this program

If the idea is still fuzzy, read Day 8: Conditional Statements in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 20 conditional programs, or see every topic on Python exercises.