Check Password Strength in Python
Long enough, with upper case, lower case and a digit? Combine several checks with and plus any().
- Checking length
- Testing characters with isupper, isdigit and friends
- Combining rules with and and any()
length ≥ 12✓uppercase✓lowercase✓digit✓all rulesstrong = TrueEvery 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.
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 tickedA password strength check combines rules such as minimum length and required character types, and passes only if every rule passes.
Key points
len(password) >= 12checks the length.any(character.isupper() for character in password)is True if at least one character is uppercase.- The checks are joined with
and, sostrongis True only when all four pass. - 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
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}")
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.
- Change the input and predict the output before running.
- Handle empty or invalid input.
- Wrap the logic in a function and add more test cases.
Common mistakes
Joining the checks with or
With or, one passing rule is enough, so weak passwords get through.
strong = len(password) >= 12 or any(c.isdigit() for c in password)
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