← Back to Blog

FOR IF Mini Project — Password Strength Checker

The problem...

Passwords like "abc123" get cracked in seconds. "P@ssw0rd!" takes considerably longer.

The difference? Variety. A strong password has uppercase, lowercase, digits, and special characters.

Your program is going to check for all four.

The idea!

Loop through every character. For each one — check what type it is. Use flags to track what you've found. At the end — give a verdict.

What you need to know first

Python strings have built-in methods to check character types:

print("A".isupper())    # True
print("a".islower())    # True
print("3".isdigit())    # True

For special characters — check if it's not a letter and not a digit:

char = "@"
print(not char.isalpha() and not char.isdigit())    # True

Your mission

Ask the user for a password. Check if it contains at least one uppercase letter, one lowercase letter, one digit, and one special character. Print the result for each check.

The solution

password = input("Enter a password: ")

has_upper = False
has_lower = False
has_digit = False
has_special = False

for char in password:
    if char.isupper():
        has_upper = True
    elif char.islower():
        has_lower = True
    elif char.isdigit():
        has_digit = True
    elif not char.isalpha() and not char.isdigit():
        has_special = True

print(f"Uppercase:        {'✓' if has_upper else '✗'}")
print(f"Lowercase:        {'✓' if has_lower else '✗'}")
print(f"Digit:            {'✓' if has_digit else '✗'}")
print(f"Special character:{'✓' if has_special else '✗'}")

if has_upper and has_lower and has_digit and has_special:
    print("Strong password.")
else:
    print("Weak password. Keep improving it.")

Test it

# Enter a password: abc123
# Uppercase:         ✗
# Lowercase:         ✓
# Digit:             ✓
# Special character: ✗
# Weak password. Keep improving it.

# Enter a password: P@ssw0rd!
# Uppercase:         ✓
# Lowercase:         ✓
# Digit:             ✓
# Special character: ✓
# Strong password.

What's really happening

The loop visits every character. The if / elif chain identifies the type. Four flags track what's been found. After the loop — the verdict is ready.

No break here — you need to check every character to be sure. That's the right call.

Go further

  • Add a minimum length check — at least 8 characters
  • Add a score — 1 point per condition met, print "Score: 3/4"
  • Use break to stop as soon as all four conditions are met

What you should understand now

  • isupper(), islower(), isdigit() check character types
  • Flags — variables set to False before the loop, flipped to True when found
  • Sometimes you don't break — you need every item checked
  • Ternary inside f-strings: {'✓' if condition else '✗'}
[ login to bookmark ] // copied! 23 views · 2 min
// resources
Exercise password_strength_checker.py
← prev The end of the loop might not be the end next → FOR IF Mini Project — Pig Latin
// 0 comments
// No comments yet. Be the first.
// leave a comment

// Your comment will appear after approval.