Start learning free

LESSON 166 OF 410 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V3 - Character Classes in Python

Practice

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

    Password Strength V3 in Python

    Keep going with PythonAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free