Categorize Password as Strong or Weak using Regex in Python Last Updated : 15 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Given a password, we have to categorize it as a strong or weak one. There are some checks that need to be met to be a strong password. For a weak password, we need to return the reason for it to be weak. Conditions to be fulfilled are: Minimum 9 characters and maximum 20 characters.Cannot be a newline or a spaceThere should not be three or more repeating characters in a row.The same string pattern(minimum of two character length) should not be repeating. Note: For checking the basic validations of a password, click here. Examples: Input1 : Qggf!@ghf3 Output1 : Strong Password! Input2 : aaabnil1gu Output2 : Weak Password: Same character repeats three or more times in a row Input3 : Geeksforgeeks Output3 : Weak Password: Same character repeats three or more times in a row Input4 : Aasd!feasnm Output4 : Weak password: Same string pattern repetition Input5 : 772*hdf77 Output5 : Weak password: Same string pattern repetition Input6 : " " Output6 : Password cannot be a newline or space! Below is the implementation. Python3 # Categorizing password as Strong or # Weak in Python using Regex import re # Function to categorize password def password(v): # the password should not be a # newline or space if v == "\n" or v == " ": return "Password cannot be a newline or space!" # the password length should be in # between 9 and 20 if 9 <= len(v) <= 20: # checks for occurrence of a character # three or more times in a row if re.search(r'(.)\1\1', v): return "Weak Password: Same character repeats three or more times in a row" # checks for occurrence of same string # pattern( minimum of two character length) # repeating if re.search(r'(..)(.*?)\1', v): return "Weak password: Same string pattern repetition" else: return "Strong Password!" else: return "Password length must be 9-20 characters!" # Main method def main(): # Driver code print(password("Qggf!@ghf3")) print(password("Gggksforgeeks")) print(password("aaabnil1gu")) print(password("Aasd!feasn")) print(password("772*hd897")) print(password(" ")) # Driver Code if __name__ == '__main__': main() OutputStrong Password! Weak password: Same string pattern repetition Weak Password: Same character repeats three or more times in a row Weak password: Same string pattern repetition Strong Password! Password cannot be a newline or space! Time Complexity: O(N) N is refers to the length of the input password string,Auxiliary Space: O(1) Comment More infoAdvertise with us A Anshika Goyal Follow Improve Article Tags : Python python-regex Python Regex-programs Practice Tags : python Explore Python FundamentalsPython Introduction 3 min read Input and Output in Python 7 min read Python Variables 6 min read Python Operators 6 min read Python Keywords 2 min read Python Data Types 9 min read Conditional Statements in Python 6 min read Loops in Python - For, While and Nested Loops 9 min read Python Functions 9 min read Recursion in Python 6 min read Python Lambda Functions 6 min read Python Data StructuresPython String 6 min read Python Lists 6 min read Python Tuples 6 min read Dictionaries in Python 7 min read Python Sets 10 min read Python Arrays 9 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 6 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 12 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 11 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 6 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 7 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 10 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 3 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like