Check a strong password

MediumRegEx Programs · 4 of 20

Write a Python program to check that a password has 8+ characters with upper, lower, digit and symbol.

Advertisement
Output
Test@2026    True
password     False
Short1!      False
NoSymbol123  False

Approach

  1. Look-ahead assertions check each rule without consuming characters.

Solution

Python
import re STRONG = re.compile(r"(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\w\s]).{8,}")for pw in ["Test@2026", "password", "Short1!", "NoSymbol123"]:    print(pw.ljust(12), bool(STRONG.fullmatch(pw)))
Output
Test@2026    True
password     False
Short1!      False
NoSymbol123  False

Line by line

Line 3

Each (?=...) checks that some character of that kind exists; .{8,} enforces the length.

Learn the concept

New to this topic? Read the lesson, then come back.

More regex programs

← All 20 RegEx Programs

Leave a Comment