Categorize Password as Strong or Weak using Regex in Python (original) (raw)

Last Updated : 17 Nov, 2025

Given a password string, the task is to check whether it is strong or weak based on a set of conditions. A password is considered strong only if it passes all rules below:

**For Examples:

**Input: Qggf!@ghf3
**Output: Strong Password!

**Input: aaabnil1gu
**Output: Weak Password -> Same character repeats three or more times in a row

Let’s explore different methods to validate password strength.

Using a Single Regex Pattern

This method uses one comprehensive regex pattern that checks password length, repeating characters, and repeating patterns in a single match. If the entire password matches the pattern, it is considered strong.

Python `

import re

password = "Qggf!@ghf3" pattern = ( r"^(?!.(.)\1\1)"
r"(?!.
(..).*\1)"
r"[^\s]{9,20}$"
)

if re.fullmatch(pattern, password): print("Strong Password!") else: print("Weak Password!")

`

**Explanation:

Using Multiple Regex Checks

Instead of one big regex, this method checks each rule separately using smaller regular expressions. If any check fails, the corresponding message is printed.

Python `

import re password = "Qggf!@ghf3"

if password == " " or password == "\n": print("Password cannot be a newline or space!") elif not (9 <= len(password) <= 20): print("Password length must be 9-20 characters!") elif re.search(r"(.)\1\1", password): print("Weak Password: Same character repeats three or more times in a row") elif re.search(r"(..)(.*?)\1", password): print("Weak password: Same string pattern repetition") else: print("Strong Password!")

`

**Explanation:

Manual Character Repetition Check + Regex for Pattern

This method uses a simple loop to detect 3 repeated characters and regex to detect repeating substring patterns. It is slower than full-regex but easier to understand.

Python `

import re password = "aaabnil1gu"

if password == " " or password == "\n": print("Password cannot be a newline or space!") elif not (9 <= len(password) <= 20): print("Password length must be 9-20 characters!") else: repeat_three = False for i in range(len(password) - 2): if password[i] == password[i+1] == password[i+2]: repeat_three = True break

if repeat_three:
    print("Weak Password: Same character repeats three or more times in a row")
elif re.search(r"(..)(.*?)\1", password):
    print("Weak password: Same string pattern repetition")
else:
    print("Strong Password!")

`

Output

Weak Password: Same character repeats three or more times in a row

**Explanation:

Pure Manual Checks Without Built-ins

This method manually checks length, spaces, repeated characters, and substring repetition without relying on regex. It is the slowest and mainly educational.

Python `

password = "Qggf!@ghf3"

if password == " " or password == "\n": print("Password cannot be a newline or space!") elif not (9 <= len(password) <= 20): print("Password length must be 9-20 characters!") else:

# Check repeating characters 3+ times
repeating_three = False
for i in range(len(password) - 2):
    if password[i] == password[i+1] == password[i+2]:
        repeating_three = True
        break

# Check repeating substring patterns
repeating_pattern = False
for i in range(len(password) - 3):
    if password[i:i+2] in password[i+2:]:
        repeating_pattern = True
        break

if repeating_three:
    print("Weak Password: Same character repeats three or more times in a row")
elif repeating_pattern:
    print("Weak password: Same string pattern repetition")
else:
    print("Strong Password!")

`

**Explanation: