Check the strength of your password using python - with regex

a python program to check the strength of any passwords using regular expressions

Introduction

Do you really think twice before setting a password for your online accounts? I'm talking about the strength of the passwords you select. For example, 'Alex0011', 'HeyBaby89'; these passwords are so common and simple. Everyone should avoid to keep passwords like these.

So, what should you do? Simply, choose a strong password. But how can you find your chosen password is truly strong or not? Don't worry! I came up here with the solution of it. You will get the treasure shortly.

A Strong Password is a very crucial part to prevent any online or offline account from being exploited or hacked easily. There are many more steps you should keep in mind to keep your account safe(Know it from here).

Nowadays the majority of online services like Google, Microsoft, Twitter, etc. suggests keeping a strong password while creating an account on their platforms.

A strong password must be complex and long. The reason behind keeping a complex password is because it takes more time and effort to guess that password by hackers.

In this tutorial, we will develop a Password Strength Checker in Python. It will check the strength of any passwords with the help of python regular expressions and display the password entered by the user is strong or weak.

Visit Also: Create a Time Wasting Websites Blocker using Python

What should a Strong Password look like?

👉 The password length should be 15 or greater. But in this program, I've taken the min. length 10 for Your convenience.

👉 The password must contain uppercase[A-Z], lowercase[a-z], numeric digits[0-9], and at least one special character.

Examples of Strong Password

  • 5rvgt&XxUr@901
  • Hey_Hel-Sinki_please_cut_ur_beard@21
  • GoogleGoogleHappyBirthday@23

The first one is hard to remember but the rest are not. These are just examples for your understanding.

The Code

The code is nothing but play with Regular Expressions. The program tries to match the characters from the password(entered by the user as an input) with the regular expressions and give an output based on the performance.


'''Password strength Checker in Python'''
import re

check_pass = re.compile(r'''(
^.*(?=.{10,}) # Checking the length, minimum 8
(?=.*\d) # Numeric Digits
(?=.*[a-z]) # Lowercase, a to z.
(?=.*[A-Z]) # Uppercase, A to Z
(?=.*[@#$%^&+=!]).*$ # Special Characters
)''',re.VERBOSE)

passCode = input('Enter the Password: ')
mo1 = check_pass.search(passCode)

# Check the password is validate or not
if mo1:
print("Strong Password.")
else:
print("Not Valid!. Weak Password.")

Output

Input no. 1

Enter the Password: IznhfyszmpzizphsrgUSA$100daily@2025
Strong Password.

Input no. 2

Enter the Password: 1234567890
Not Valid!. Weak Password.

Input no. 3

Enter the Password: GoogleGoogleHappyBirthday@23
Strong Password.

Input no. 4

Enter the Password: Hey_Hel-Sinki_please_cut_ur_beard@21
Strong Password.

Output Video

Important Note
Please read about the Regular Expression in Python to understand how actually regular expressions work.

Illustration of the Code

The code is not too complex. But you should understand only the regex part from there because it's the most crucial part that helps to find out the strength of any passwords.

check_pass = re.compile(r'''(
🅐^.*(?=.{10,}) # Checking the length, minimum 8
🅑(?=.*\d) # Numeric Digits
🅒(?=.*[a-z]) # Lowercase, a to z.
🅓(?=.*[A-Z]) # Uppercase, A to Z
🅔(?=.*[@#$%^&+=!]).*$ # Special Characters
)''',re.VERBOSE)

🅐 Here, at the very first line ^ (pronunciation: Caret) matches the start of the string.

🅐 .*(Dot-Star) indicating that there can be zero or more number of characters before the matching expression

🅐 (?=__) It's a looking-ahead approach. It matches if __ matches in the next. OK, let's take an example: 'Heist (?=Money)'  will match 'Money' only if 'Heist' is followed by 'Money'. I hope now you can understand the rest of the part

🅐 .{10,} checks the minimum length. In this case, the min. length is 10

So, the line 🅐 summaries that, it matches any characters if the minimum length of the search string is 10 from the start of the string.

The next three lines follow almost the same theory. Please see the comment section there(mentioned at the right side of each line).

🅔 This line matches any special character mentioned there. .*$ matches zero or more number of characters before the end of the string or the newline.

Visit AlsoHack with Image: Extract Metadata from an Image using Python

Conclusion

Regular Expressions makes a program easy to find a pattern in a search string. It would be easy when you can understand all the syntax properly. 

Today we build a Password Strength Checker in Python with the help of Regular Expressions here. It helps find if a password is truly strong or not.

So, what do you think, is this program gonna help you? Let me know in the comment section. For any query you can ask me too. I will get back to you with the answer soon.

Thanks for reading!💙

PySeek

Subhankar Rakshit

Meet Subhankar Rakshit, a Computer Science postgraduate (M.Sc.) and the creator of PySeek. Subhankar is a programmer, specializes in Python language. With a several years of experience under his belt, he has developed a deep understanding of software development. He enjoys writing blogs on various topics related to Computer Science, Python Programming, and Software Development.

Post a Comment (0)
Previous Post Next Post