Beginner Python Exercises

Check Password Strength in Python

Long enough, with upper case, lower case and a digit? Combine several checks with and plus any().

  • Beginner
  • 2 min read
  • 1 runnable example
What you'll learn
  • Checking length
  • Testing characters with isupper, isdigit and friends
  • Combining rules with and and any()
"CodeUtility2026"
length ≥ 12✓
uppercase✓
lowercase✓
digit✓
all rulesstrong = True

Every rule must pass

The problem

Sign-up forms must refuse weak passwords such as "12345" or "password". A good rule checks several things at once: length and the presence of different kinds of characters. Each check is simple; the skill is combining them correctly.

Think of it like this

Checking a password is like the checklist under a sign-up form, with four boxes: at least 12 characters, a lowercase letter, an uppercase letter and a digit. The password is accepted only when every box is ticked. One empty box is enough to refuse it.

From a sign-up checklist to code
len(password) >= 12📏 Long enough?
any(character.islower() for ...)🔡 At least one lowercase letter?
any(character.isdigit() for ...)🔢 At least one digit?
and☑️ Every box must be ticked
One failed check is enough to reject the password.
In one sentence

A password strength check combines rules such as minimum length and required character types, and passes only if every rule passes.

Key points

  1. len(password) >= 12 checks the length.
  2. any(character.isupper() for character in password) is True if at least one character is uppercase.
  3. The checks are joined with and, so strong is True only when all four pass.
  4. The parentheses let the long condition span several lines.

When should you use it?

  • Validating passwords and other input in sign-up forms.
  • Practicing any(), string methods and combined conditions.
  • Giving users feedback on which rule failed.

When not to use it

  • Real security on its own: length and blocking common passwords matter more than character rules, and passwords must be stored hashed.
  • Telling users exactly what is missing: check each rule separately instead of one combined expression.

Example code

Run code →
main.py
password = "CodeUtility2026"
# Require length and three different character classes.
strong = (
    len(password) >= 12
    and any(character.islower() for character in password)
    and any(character.isupper() for character in password)
    and any(character.isdigit() for character in password)
)
print(f"strong={strong}")
Get a Run button for this example

Result of running the code

strong=True

Code explanation

Each line of the condition checks one rule: the length, then at least one lowercase letter, one uppercase letter and one digit, using any() with a string method. The rules are joined with and, so a single failing rule makes strong False. The surrounding parentheses allow the long condition to be split over several readable lines.

Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.

Practice exercises

Modify the runnable example with the exercises below to build understanding beyond copying the result.

  1. Change the input and predict the output before running.
  2. Handle empty or invalid input.
  3. Wrap the logic in a function and add more test cases.
Run in the Python compiler →

Common mistakes

Joining the checks with or

With or, one passing rule is enough, so weak passwords get through.

Problem
strong = len(password) >= 12 or any(c.isdigit() for c in password)
Fix
strong = len(password) >= 12 and any(c.isdigit() for c in password)

Finished this lesson?

Mark it as learned to track your progress.

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges