Start learning free

LESSON 195 OF 673 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V4 in Python: Counting Character Classes

Practice Read the full explanation

Count how many characters of each class the password contains.

You start with:

password = "Hello!World9"

Build:

Uppercase: 2
Lowercase: 8
Digits: 1
Symbols: 1

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.
  • Use four integer counters, all starting at 0.
  • Loop through each character and add 1 to the matching counter.
  • Treat any character that is not a letter and not a digit as a symbol.
  • Add the line one line per counter, in this order: "Uppercase: ", "Lowercase: ", "Digits: ", "Symbols: ", each followed by the count.

Try other passwords: with Pass word!2 the output is

Uppercase: 1
Lowercase: 7
Digits: 1
Symbols: 2

Use += 1 instead of setting flags.

password = "Hello!World9"

uppercase = 0
lowercase = 0
digits = 0
symbols = 0
for c in password:
    if c.isupper():
        uppercase += 1
    if c.islower():
        lowercase += 1
    if c.isdigit():
        digits += 1
    if not c.isalnum():
        symbols += 1
lines = []
lines.append("Uppercase: " + str(uppercase))
lines.append("Lowercase: " + str(lowercase))
lines.append("Digits: " + str(digits))
lines.append("Symbols: " + str(symbols))
receipt = "\n".join(lines)

STEP 1 OF 15
    Ctrl + Enter to check

    PYTHON · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V4 in Python, explained

    Problem overview

    Version 4 stops asking whether a kind of character is present and counts how many characters of each kind there are. The starting code sets password = "Hello!World9". The program uses four integer counters, uppercase, lowercase, digits and symbols, and stores four lines in receipt. A symbol is any character that is neither a letter nor a digit. There is no verdict line in this version.

    Uppercase: 2
    Lowercase: 8
    Digits: 1
    Symbols: 1

    The password has 12 characters, and 2 + 8 + 1 + 1 is 12, so every character has been counted once.

    Approach: counters instead of flags

    A flag remembers "at least one". A counter remembers "how many", so the assignment = True becomes += 1. Each counter starts at 0. The loop visits every character, and each of four if statements adds 1 to the counter that matches.

    After the loop, the program builds one line per counter in a fixed order and joins the lines.

    Step by step

    1. Create four counters at 0: uppercase, lowercase, digits and symbols.
    2. Loop over the characters with for c in password. For this password, the loop body runs 12 times.
    3. Count uppercase letters. When c.isupper() is true, uppercase += 1.
    4. Count lowercase letters. When c.islower() is true, lowercase += 1.
    5. Count digits. When c.isdigit() is true, digits += 1.
    6. Count symbols. When not c.isalnum() is true, symbols += 1. A space counts too.
    7. Create the empty list lines below the loop.
    8. Append the four lines in the order Uppercase, Lowercase, Digits, Symbols. Each is a label followed by the counter turned into text with str().
    9. Create receipt. "\n".join(lines).

    Watching the counters change

    Here is the loop on the password "Ab1!x", with the four counters printed after each character, in the order uppercase, lowercase, digits, symbols:

    'A' 1 0 0 0
    'b' 1 1 0 0
    '1' 1 1 1 0
    '!' 1 1 1 1
    'x' 1 2 1 1

    Each character raises exactly one counter. That gives a useful self-check: the four counters add up to len(password), here 5. The sum equals the length for ordinary passwords, though a letter without a case, such as one in "日本語", is counted by none of the four.

    The solution in Python

    password = "Hello!World9"
    
    uppercase = 0
    lowercase = 0
    digits = 0
    symbols = 0
    for c in password:
        if c.isupper():
            uppercase += 1
        if c.islower():
            lowercase += 1
        if c.isdigit():
            digits += 1
        if not c.isalnum():
            symbols += 1
    lines = []
    lines.append("Uppercase: " + str(uppercase))
    lines.append("Lowercase: " + str(lowercase))
    lines.append("Digits: " + str(digits))
    lines.append("Symbols: " + str(symbols))
    receipt = "\n".join(lines)
    

    Details worth knowing:

    • += 1. It is shorthand for uppercase = uppercase + 1. It works only on a variable that already has a value, which is why the counters are created before the loop.
    • Each test is its own if. A character is examined by all four tests, and at most one of them fires for it.
    • Labels differ from version 3. The lines are Digits and Symbols in the plural, and Uppercase and Lowercase in full, so copy the label text carefully.
    • Letters from other alphabets. The methods work on Unicode text. In "ÉÀé1" the counts are 2 uppercase, 1 lowercase, 1 digit and 0 symbols. Characters that are letters without a case, such as "日本語", are alphanumeric but neither upper nor lower, so they go in none of the four counters.
    • Counters are plain integers. They need str() to be joined with a label, like every number in these lessons.

    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. Building the four output lines is constant work.

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

    From flags to counts

    The structure is the same as in the flag version: variables created before the loop, one pass, tests inside the loop, output after it. Only the update changes. A flag records that something happened, and a counter records how many times. Counting loses nothing, because a count above 0 answers the question "is there at least one?" as well. The reverse isn't true: a flag can't tell 1 from 8.

    Edge cases and common mistakes

    • Spaces. "Pass word!2" has 1 uppercase letter, 7 lowercase letters, 1 digit and 2 symbols: the space and the exclamation mark.
    • The empty password. The loop doesn't run, and all four lines show 0.
    • Starting a counter at 1. Every count would be one too high.
    • Creating counters inside the loop. They would reset to 0 on every character.
    • Writing the symbol test as not c.isalpha(). Digits aren't letters, so every digit would also be counted as a symbol. The test must be not c.isalnum().
    • Counting with len. len(password) is the total, not the count of one kind. Each counter has to be raised one character at a time, inside its own test.
    • Appending lines inside the loop. The program would produce four lines for every character.

    Testing

    • "Hello!World9" gives 2, 8, 1 and 1.
    • "Pass word!2" gives 1, 7, 1 and 2.
    • "" gives 0, 0, 0 and 0.
    • "ÉÀé1" gives 2, 1, 1 and 0.
    Keep going with Python650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free