Start learning free

LESSON 193 OF 287 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V3 in Python: Four Character-Class Flags

Practice Read the full explanation

Track four boolean flags for a password: does it contain an uppercase letter, a lowercase letter, a digit, and a symbol (non-alphanumeric)?

You start with:

password = "Secret!99"

Build:

Length: 9
Upper: True
Lower: True
Digit: True
Symbol: True
Strong

Rules:

  • Collect the output lines in a variable named receipt: one string with the lines joined by "\n" (for example build a list lines and join it). The grader prints receipt for you, so do not call print.
  • Add the line "Length: " followed by the number of characters.
  • Start four flags (has_upper, has_lower, has_digit, has_symbol) at False. Loop over every character and set the matching flag to True.
  • A symbol is any character that is not a letter and not a digit.
  • Add the line one line per flag, in this order: "Upper: ", "Lower: ", "Digit: ", "Symbol: ", each followed by the flag.
  • If the length is at least 8 and all four flags are True (stricter than the last lesson), add the line Strong; otherwise add the line Weak.

Try other passwords: with secret99 the output is

Length: 8
Upper: False
Lower: True
Digit: True
Symbol: False
Weak

c.isupper(), c.islower(), c.isdigit(), and not c.isalnum() cover the four classes.

password = "Secret!99"

length = len(password)
lines = []
lines.append("Length: " + str(length))
has_upper = False
has_lower = False
has_digit = False
has_symbol = False
for c in password:
    if c.isupper():
        has_upper = True
    if c.islower():
        has_lower = True
    if c.isdigit():
        has_digit = True
    if not c.isalnum():
        has_symbol = True
lines.append("Upper: " + str(has_upper))
lines.append("Lower: " + str(has_lower))
lines.append("Digit: " + str(has_digit))
lines.append("Symbol: " + str(has_symbol))
if length >= 8 and has_upper and has_lower and has_digit and has_symbol:
    lines.append("Strong")
else:
    lines.append("Weak")
receipt = "\n".join(lines)

STEP 1 OF 15
    Ctrl + Enter to check

    PYTHON · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V3 in Python, explained

    Problem overview

    Version 3 asks four questions about the password instead of one: does it contain an uppercase letter, a lowercase letter, a digit and a symbol? A symbol is any character that is neither a letter nor a digit. The starting code sets password = "Secret!99". The receipt in receipt shows the length, one line per flag in the order Upper, Lower, Digit, Symbol, and a verdict. The verdict is now stricter: Strong needs a length of at least 8 and all four flags true.

    Length: 9
    Upper: True
    Lower: True
    Digit: True
    Symbol: True
    Strong

    Approach: one pass, four independent tests

    The previous version used one flag, has_digit. This one uses four flags named has_upper, has_lower, has_digit and has_symbol, all starting at False. A single loop over the characters tests each character against all four kinds.

    The tests are four separate if statements, not an if/elif chain. Each character falls into one kind at most, but the program still tests every kind for it, and the tests don't interfere. A character that matches no test, such as a space, leaves every flag as it was.

    Step by step

    1. Create length and lines. Then append Length: followed by str(length).
    2. Create the four flags as False: has_upper, has_lower, has_digit and has_symbol.
    3. Loop over the characters with for c in password.
    4. Test for uppercase. c.isupper() sets has_upper.
    5. Test for lowercase. c.islower() sets has_lower.
    6. Test for a digit. c.isdigit() sets has_digit.
    7. Test for a symbol. not c.isalnum() sets has_symbol. isalnum() is true for letters and digits, so the not leaves everything else.
    8. Append the four flag lines after the loop, in the order Upper, Lower, Digit, Symbol, each with str() on the flag.
    9. Write the verdict. One if joins five conditions with and: the length and the four flags. The else appends Weak.
    10. Create receipt. "\n".join(lines).

    Watching the four flags change

    Here is the loop on the short password "Aa1!", with the four flags printed after each character, in the order has_upper, has_lower, has_digit, has_symbol:

    'A' True False False False
    'a' True True False False
    '1' True True True False
    '!' True True True True

    Each character switches on exactly one flag, and no later character switches one off.

    The solution in Python

    password = "Secret!99"
    
    length = len(password)
    lines = []
    lines.append("Length: " + str(length))
    has_upper = False
    has_lower = False
    has_digit = False
    has_symbol = False
    for c in password:
        if c.isupper():
            has_upper = True
        if c.islower():
            has_lower = True
        if c.isdigit():
            has_digit = True
        if not c.isalnum():
            has_symbol = True
    lines.append("Upper: " + str(has_upper))
    lines.append("Lower: " + str(has_lower))
    lines.append("Digit: " + str(has_digit))
    lines.append("Symbol: " + str(has_symbol))
    if length >= 8 and has_upper and has_lower and has_digit and has_symbol:
        lines.append("Strong")
    else:
        lines.append("Weak")
    receipt = "\n".join(lines)
    

    Details worth knowing:

    • What each method returns for one character. For "S", isupper() is true and islower() is false. For "5", isdigit() is true and isalnum() is true, so it isn't a symbol. For "!", all four of isupper(), islower(), isdigit() and isalnum() are false, so not c.isalnum() is true.
    • Why four if statements. With elif, a character would stop at the first test that matched. That happens to work for these tests, but four independent if statements keep each test self-contained, and they stay correct if the tests ever overlap.
    • A space is a symbol. It isn't alphanumeric, so "Secret 99" gets Symbol: True and is Strong.
    • The long condition. A flag is already a boolean, so and has_upper needs no == True.
    • Booleans become text with str(). str(True) is "True".

    Complexity

    Time: O(n), where n is the number of characters. The loop makes one pass, and each character gets a fixed number of constant-time tests. The four tests per character don't change that, because four is a constant.

    Space: O(1) extra for the flags or counters and the few output lines, whatever the password's length.

    A stricter verdict

    Version 2 needed two conditions for Strong: the length and a digit. This version needs five, so the verdict line is the longest in the program. Every added requirement can only turn a Strong into a Weak, never the other way. A password that passed before, like "Secret99", fails now because it has no symbol.

    Why four separate variables

    Four named flags read better than one clever value, and each can be reported on its own line. The verdict also reads almost like the rule: the length is at least 8, and the password has an uppercase letter, and a lowercase letter, and a digit, and a symbol. When a password is weak, the four flag lines show which kinds are missing, and the length line shows whether it is long enough.

    Edge cases and common mistakes

    • Missing a symbol. "Secret99" has length 8 and three flags true, but Symbol: False makes it Weak.
    • Missing a digit. "Secret!!!" has Digit: False and is Weak.
    • Everything but the length. "Aa1!" has all four flags true, but it has only 4 characters, so it is Weak.
    • All lowercase. "secret99" gives Upper: False, Lower: True, Digit: True, Symbol: False and Weak.
    • Treating not c.isalpha() as "symbol". Digits aren't letters either, so every digit would count as a symbol.
    • Forgetting one flag in the verdict. With only four conditions, a password could be missing the omitted kind and still be Strong.
    • Using or in the verdict. With or, a single true flag would be enough for Strong.
    • Swapping the line order. The lines have to appear as Upper, Lower, Digit, Symbol.

    Testing

    • "Secret!99" gives four True lines and Strong.
    • "secret99" gives Upper: False, Lower: True, Digit: True, Symbol: False and Weak.
    • "Secret99" gives Symbol: False and Weak.
    • "Aa1!" gives four True lines, Length: 4 and Weak.
    • "Secret 99" gives four True lines and Strong.
    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free