Start learning free

LESSON 192 OF 287 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V2 in Python: Length Plus a Digit Flag

Practice Read the full explanation

Extend the length check with a second rule: the password must also contain at least one digit.

You start with:

password = "Secret99"

Build:

Length: 8
Has digit: 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.
  • Loop through each character to set a boolean flag has_digit, which starts as False and becomes True when a digit is seen.
  • Add the line "Has digit: " followed by the flag.
  • If the length is at least 8 and has_digit is True, add the line Strong; otherwise add the line Weak.

Try other passwords: with abcdefgh the output is

Length: 8
Has digit: False
Weak

Use c.isdigit() in a loop to detect digits.

password = "Secret99"

length = len(password)
lines = []
lines.append("Length: " + str(length))
has_digit = False
for c in password:
    if c.isdigit():
        has_digit = True
lines.append("Has digit: " + str(has_digit))
if length >= 8 and has_digit:
    lines.append("Strong")
else:
    lines.append("Weak")
receipt = "\n".join(lines)

STEP 1 OF 10
    Ctrl + Enter to check

    PYTHON · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V2 in Python, explained

    Problem overview

    Version 2 keeps the length check from version 1 and adds a second rule: a strong password must also contain at least one digit. The starting code sets password = "Secret99". The receipt, stored in receipt, has three lines: the length, whether a digit was found, and the verdict, Strong or Weak.

    Length: 8
    Has digit: True
    Strong

    For "abcdefgh" the length passes, but there is no digit, so the lines are Length: 8, Has digit: False and Weak.

    Approach: a boolean flag set by a loop

    To know whether any character is a digit, the program has to look at every character. The usual pattern is a flag: a boolean that starts as False ("no digit seen yet") and is set to True the moment a digit appears. A loop over the password visits each character once and tests it.

    The flag only ever moves from False to True. No branch sets it back, so one digit anywhere in the password is enough, even if later characters are letters.

    Step by step

    1. Create length. len(password), used in the first line and in the verdict.
    2. Create the empty list lines.
    3. Append the length line. Length: followed by str(length).
    4. Create has_digit as False. The assumption is that there is no digit until one is found.
    5. Loop over the characters. for c in password gives c one character per pass.
    6. Test each character. When c.isdigit() is true, set has_digit to True.
    7. Append the flag line after the loop. The line is not indented, so it runs once, when every character has been checked. str() of a boolean gives the text True or False, which is what the output shows.
    8. Write the verdict's if. When length >= 8 and has_digit, append Strong. Both conditions must hold.
    9. Write the else. It appends Weak for everything else: too short, no digit, or both.
    10. Create receipt. "\n".join(lines).

    Watching the flag change

    Here is the loop on the password "ab9c", with the value of has_digit printed after each character:

    'a' False
    'b' False
    '9' True
    'c' True

    The flag flips at the digit and stays true when the letter c follows. The final value is what the second output line reports.

    The solution in Python

    password = "Secret99"
    
    length = len(password)
    lines = []
    lines.append("Length: " + str(length))
    has_digit = False
    for c in password:
        if c.isdigit():
            has_digit = True
    lines.append("Has digit: " + str(has_digit))
    if length >= 8 and has_digit:
        lines.append("Strong")
    else:
        lines.append("Weak")
    receipt = "\n".join(lines)
    

    Python can also fold the loop and the flag into one expression. any(c.isdigit() for c in password) is True for "Secret99" and False for "abcdefgh". The explicit loop in this lesson does the same work and shows each step.

    Details worth knowing:

    • Looping over a string. A for loop over a string produces its characters one at a time, and each is a one-character string.
    • isdigit(). It returns True for a one-character string such as "9" and False for "a" or "!". It is a method of the string, so it is called as c.isdigit(), with parentheses.
    • It isn't limited to 0 to 9. isdigit() also accepts digits from other scripts. With the Arabic-Indic digits "٣٤٥٦٧٨٩٠", the flag is True and the verdict is Strong. The lesson's passwords use ordinary digits.
    • The flag keeps its value. Because the if has no else, a letter after a digit can't reset the flag.
    • and needs both sides true. If either side is false, the whole condition is false and the else branch runs.

    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 line building and the verdict after the loop take the same few steps for any password.

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

    Reading the receipt

    The verdict alone doesn't say why a password is weak. The two lines above it do. Length: 4 with Has digit: True means the password was too short, while Length: 8 with Has digit: False means it had no digit. Printing the facts before the verdict makes the receipt useful as a diagnosis, and it makes each fact easy to check on its own.

    Edge cases and common mistakes

    • Long, but no digit. "abcdefgh" gives Has digit: False and Weak.
    • A digit, but too short. "abc1" gives Length: 4, Has digit: True and Weak.
    • Only digits. "12345678" is Strong in this version, because the only rules are the length and a digit.
    • Resetting the flag in an else. Adding else: has_digit = False inside the loop would erase the result whenever the last character isn't a digit.
    • Printing the flag line inside the loop. It would be appended once per character.
    • Writing c = isdigit() or isdigit(c). The method belongs to the string: c.isdigit().
    • Checking only the first or last character. A test like password[0].isdigit() looks at one position. The loop is what makes a digit anywhere in the password count.
    • Forgetting str() on the flag. "Has digit: " + has_digit raises a TypeError, because a boolean is not text.

    Testing

    • "Secret99" gives Length: 8, Has digit: True and Strong.
    • "abcdefgh" gives Length: 8, Has digit: False and Weak.
    • "abc1" gives Length: 4, Has digit: True and Weak.
    • "12345678" gives Length: 8, Has digit: True 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