Creating a Simple Password Strength Checker: In Python

Michael Lopez
3 min readDec 26, 2023

--

Photo Credit:https://en.wikipedia.org/wiki/Python_%28programming_language%29

In this blog i’m going to show you how i created this script in python that prompts the user to enter password and checks against a set of predefined rules for password strength. Also maybe someone create something and add your own twist to it.

Things we need to implement:

  1. Minimum length of 12 characters.
  2. At least one lowercase letter.
  3. At least one uppercase letter.
  4. At least one digit.
  5. At least one special character.
#Password Strength Checker Program
import re

#Password must be at least 12 charcters long
password = input ("Enter Your Password. ")
if len(password) < 12:
print("Password must be at least 12 characters long.")
#Password must contain at least one uppercase letter.
elif not re.search("[A-Z]", password):
print("Password must contain at least one uppercase letter.")
#Password must contain at least one lowercase letter.
elif not re.search("[a-z]", password):
print("Password must contain at least one lowercase letter.")
#Password must contain at least one number
elif not re.search("[0-9]", password):
print("Password must contain at least one digit.")
#Password must contain at least one special character.
special_characters = re.compile(r"[@_!#$%^&*()<>?/\|}{~:].")
if not re.search("[!@#$%^&*(),.?\":{}|<>]", password):
print ("Password should contain at least one special character.")
else:
("Strong: Password meets the criteria.")

Meaning of Keywords:

1. Import re. Line 2

Re: A regular expression (or RE) specifies a set of strings that matches it; the functions in this module let you check if a particular string matches a given regular expression (or if a given regular expression matches a particular string, which comes down to the same thing).

2.Input function: Ex: password = input (“Enter Your Password. “) Line 5

The input function is used to ask the user of the program (not the programmer) a question, and then wait for a typed response. The typed number is then returned as the result of the function, and should usually be stored in a variable.

3.Using “if”: Ex: if len(password) < 12: Llines 6, and 19.

An “if” statement is a condition statement used to check a condition, and execute it if the condition holds true. It is also a control flow statement, which utilizes decision-making to control the flow of execution.

4. Print(): EX: print(“Password must be at least 12 characters long.”) Lines 7,10,13,16,and 20.

The print() function prints the specified message to the screen, or other standard output device. The message can be a string, or any other object, the object will be converted into a string before being written to the screen.

5.elif: Ex: elif not re.search(“[A-Z]”, password): Lines 9,12, and 15.

elif is short for “else if” and is used when the first if statement isn’t true, but you want to check for another condition.

6. Else: Ex (“Strong: Password meets the criteria.”)

The else keyword is used in conditional statements (if statements), and decides what to do if the condition is False.

[In The Terminal] it Shows Password Input and Checks the password against Predefined Rules for strength.

The Results: Checks Out.

--

--

Michael Lopez
0 Followers

I'm just a University and CompTIA Certified Cyber Security Professional That Loves Learning All Things Tech.