How to Check if Password is Strong or Weak in Python

In this tutorial, we will program "How to Check if a Password is Strong or Weak in Python." We’ll focus on validating a password to determine whether it meets the requirements for strength. The objective is to safely check if the entered password passes the necessary qualifications. A sample program will be provided to demonstrate the coding process effectively.

This topic is straightforward to understand. Just follow the instructions I provide, and you'll be able to complete it yourself with ease. The program I'll show you demonstrates the correct way to validate a password to determine if it is strong or weak. I'll also provide a simple and efficient method to achieve this effectively. So, let’s start coding!

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. import re
  2.  
  3.  
  4.  
  5. def password(v):
  6.  
  7.         if v == "\n" or v == " ":
  8.                 return "Password cannot be a newline or space!"
  9.  
  10.         if 9 <= len(v) <= 20:
  11.  
  12.        
  13.                 if re.search(r'(.)\1\1', v):
  14.                         return "Weak Password: Same character repeats three or more times in a row"
  15.  
  16.        
  17.                 if re.search(r'(..)(.*?)\1', v):
  18.                         return "Weak password: Same string pattern repetition"
  19.  
  20.                 else:
  21.                         return "Strong Password!"
  22.  
  23.         else:
  24.                 return "Password length must be 9-20 characters!"
  25.  
  26.  
  27. def main():
  28.         while True:
  29.                 print("\n================= Check if Password is Strong or Weak =================\n\n")
  30.                 mypass=input("Please enter your password here: ")
  31.                 print(password(mypass))
  32.                 opt = input("\nDo you want to try again?(yes/no): ")
  33.  
  34.                 if opt.lower() == 'yes':
  35.                         ret=False
  36.                 elif opt.lower() == 'no':
  37.                         ret=True
  38.                         print("Exiting program....")
  39.                 else:
  40.                         print("Please enter yes/no:")
  41.                         break
  42.  
  43.                 if ret == False:
  44.                         continue
  45.  
  46.  
  47.  
  48. if __name__ == '__main__':
  49.         main()

This program evaluates a user-entered password for strength, marking it as "Strong" if it meets the criteria of being 9 to 20 characters long, containing no repeated characters in a row or repeated patterns, and not consisting solely of newlines or spaces. If the password fails any of these checks, it is marked "Weak," with an explanation provided. The program runs continuously, prompting the user to try again until they choose to exit.

Output:

There you have it we successfully created How to Check if Password is Strong or Weak in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials

Add new comment