Intermediate · Projects: put it all together · topic 35 of 50 · 30 min
Project: password strength checker
Explanation
The plan
| Part | For our checker |
|---|---|
| Input | One password (a string) |
| Outputs | A score, a label (weak / medium / strong) and a list of reasons to improve |
| Steps | test each rule, add up the score, pick a label, collect messages for failed rules |
The rules
| Rule | How to test it in Python |
|---|---|
| At least 8 characters | len(pw) >= 8 |
| A small letter | any(c.islower() for c in pw) |
| A capital letter | any(c.isupper() for c in pw) |
| A digit | any(c.isdigit() for c in pw) |
| A symbol like @ # ! ? | any(not c.isalnum() and not c.isspace() for c in pw) |
| Bonus: 12 or more characters | len(pw) >= 12 |
Step 1: test each rule
def rules_met(pw):
return {
"length": len(pw) >= 8,
"lower": any(c.islower() for c in pw),
"upper": any(c.isupper() for c in pw),
"digit": any(c.isdigit() for c in pw),
"symbol": any(not c.isalnum() and not c.isspace() for c in pw),
}
print(rules_met("mango123"))
print(rules_met("Mango@2026"))Step 2: score, label and reasons
COMMON = {"password", "123456", "12345678", "qwerty", "india@123", "password@123"}
MESSAGES = {
"length": "Use at least 8 characters",
"lower": "Add a small letter",
"upper": "Add a capital letter",
"digit": "Add a digit",
"symbol": "Add a symbol like @ or #",
}
def rules_met(pw):
return {
"length": len(pw) >= 8,
"lower": any(c.islower() for c in pw),
"upper": any(c.isupper() for c in pw),
"digit": any(c.isdigit() for c in pw),
"symbol": any(not c.isalnum() and not c.isspace() for c in pw),
}
def check_password(pw):
rules = rules_met(pw)
score = sum(rules.values()) + (1 if len(pw) >= 12 else 0)
reasons = [MESSAGES[name] for name, ok in rules.items() if not ok]
if pw.lower() in COMMON:
return {"score": 0, "label": "weak", "reasons": ["This is a very common password"]}
if score <= 2:
label = "weak"
elif score <= 4:
label = "medium"
else:
label = "strong"
return {"score": score, "label": label, "reasons": reasons}
for pw in ["abc", "mango123", "Mango@2026", "Monsoon-Chai-2026", "India@123"]:
print(pw, check_password(pw))The score goes from 0 to 6: one point for each of the five rules, and one bonus point for 12 or more characters. 0 to 2 is weak, 3 or 4 is medium, 5 or 6 is strong. Common passwords are always weak, because attackers try them first.
Never store plain passwords
A website must check your password at login, but it must never keep the password itself. If the database is ever stolen, plain passwords would be exposed. Instead the site stores a hash. A hash is a fixed-length fingerprint made from the text. The same text always gives the same hash, but you cannot turn the hash back into the text.
import hashlib
fingerprint = hashlib.sha256("Mango@2026".encode()).hexdigest()
print(fingerprint)
print(len(fingerprint)) # always 64 hex characters
print(hashlib.sha256("mango@2026".encode()).hexdigest() == fingerprint) # False: one letter changedStep 3: test it
- An empty string should be weak with all five reasons.
- "Mango@2026" meets all five rules but is under 12 characters: score 5, strong.
- A common password like "Password" (any case) should be weak.
- A long phrase with spaces: spaces are not counted as symbols.
Watch the lesson
Project: password strength checker · 1/6
In this project you will build a password strength checker, like the meter on a sign-up form.
Prefer a video by a teacher? Find free videos on “python password strength checker project” on YouTube ↗
In real life: A sign-up form for a college fest
Students building the registration site for their college fest add a password meter under the password box. As the user types, it shows weak, medium or strong in red, orange or green, and a short list like "Add a digit". The rules are exactly our check_password function; the website just shows its result.
pw = "Rangoli2026"
missing = []
if not any(c.isdigit() for c in pw):
missing.append("Add a digit")
if not any(not c.isalnum() and not c.isspace() for c in pw):
missing.append("Add a symbol like @ or #")
print(missing)Practice (0/3 done)
Write count_rules(pw) that returns how many of these 5 rules the password meets: at least 8 characters; has a small letter; has a capital letter; has a digit; has a symbol (a character that is not a letter, not a digit and not a space).
Write strength_label(score) for a score from 0 to 6: return "weak" for 0 to 2, "medium" for 3 or 4, and "strong" for 5 or 6.
Write reasons(pw) that returns a list of messages for the rules the password FAILS, in this order: "Use at least 8 characters", "Add a small letter", "Add a capital letter", "Add a digit", "Add a symbol". A password that passes everything gives [].
Quick quiz
Read more (free)
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.