Python Practice › RegEx Programs › Program 4
Check a strong password
Write a Python program to check that a password has 8+ characters with upper, lower, digit and symbol.
Output
Test@2026 True password False Short1! False NoSymbol123 False
Approach
- Look-ahead assertions check each rule without consuming characters.
Solution
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.