Start learning free

LESSON 192 OF 673 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V1 in Python: Checking the Length

Practice Read the full explanation

A tiny password strength checker that looks only at the length of the password.

You start with:

password = "Secret99"

Build:

Password length: 8
OK

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 "Password length: " followed by the number of characters.
  • If the length is at least 8, add the line OK; otherwise add the line Too short.

Try other passwords: with abc the output is

Password length: 3
Too short

Use len(password) to get the length and str(...) to concatenate it with a string.

password = "Secret99"

length = len(password)
lines = []
lines.append("Password length: " + str(length))
if length >= 8:
    lines.append("OK")
else:
    lines.append("Too short")
receipt = "\n".join(lines)

STEP 1 OF 6
    Ctrl + Enter to check

    PYTHON · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V1 in Python, explained

    Problem overview

    Version 1 of the password strength checker looks at one thing only: how many characters the password has. The starting code sets password = "Secret99". The program must report the length and then give a verdict: OK for 8 characters or more, Too short otherwise. The output is collected in a string named receipt, with the lines separated by newline characters. The lesson shows receipt for you, so the code doesn't call print.

    For "Secret99" the receipt is:

    Password length: 8
    OK

    For "abc" it is Password length: 3 followed by Too short.

    Approach: measure once, use twice

    The length appears in the first output line and in the comparison with 8, so it is computed once with len(password) and stored in length. The lines are collected in a list named lines. The verdict is a choice between two outcomes, which is an if/else. A final "\n".join(lines) turns the list into the single string the lesson expects.

    Step by step

    1. Create length. len(password) counts the characters.
    2. Create the empty list lines. The output lines are added to it one at a time.
    3. Append the first line. The label Password length: followed by length turned into text with str(). The label ends with a space, so the number is separated from the colon.
    4. Write the if. When length >= 8, append OK. The comparison includes 8, so a password of exactly 8 characters is accepted.
    5. Write the else. It appends Too short for every length the if didn't take, which is 7 or fewer.
    6. Create receipt. "\n".join(lines) glues the lines together with a newline between each two.

    The solution in Python

    password = "Secret99"
    
    length = len(password)
    lines = []
    lines.append("Password length: " + str(length))
    if length >= 8:
        lines.append("OK")
    else:
        lines.append("Too short")
    receipt = "\n".join(lines)
    

    Details worth knowing:

    • len counts characters. A space is a character like any other, so a password made of eight spaces has length 8 and gets OK. This version doesn't judge what the characters are.
    • str() is required. "Password length: " + length raises TypeError: can only concatenate str (not "int") to str, because + won't join text and an integer.
    • join goes on the separator. The string before .join is placed between the elements. With the two lines of this lesson, the receipt is 'Password length: 8\nOK', with no newline at the end.
    • Exactly one branch runs. The if and the else together always append exactly one verdict line.

    Edge cases and common mistakes

    • The boundary. A password of 8 characters gives OK and one of 7 gives Too short. Using > instead of >= would reject the 8-character one.
    • The empty password. With password = "", the length is 0 and the receipt is Password length: 0 followed by Too short.
    • Calling print. The lesson reads receipt, so the program only has to build it.
    • Appending the verdict outside the branches. Appending OK unconditionally would put it in every receipt.
    • Forgetting the join. receipt would be a list, not a string.
    • Joining with "" or " ". The lines would sit on one row instead of two.

    Testing

    • "Secret99" gives length 8 and OK.
    • "abc" gives length 3 and Too short.
    • "abcdefg" gives length 7 and Too short.
    • "abcdefgh" gives length 8 and OK.
    • "" gives length 0 and Too short.
    Keep going with Python650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free