GITHUB REPO | FOLLOW ME ON -> TWITTER | LINKEDIN

Create Your Own Random Password Generator Using Python: A Project for Beginner

Are you tired of using weak passwords which harm your accounts? With just a few lines of code, Python’s Random Password Generator may assist you in creating a strong and secure password. We’ll guide you through the code step by step and teach you how to modify your password by deciding how many letters, numbers, and symbols to add. Let’s get started!

Al-Junaed Islam

--

Photo by Markus Spiske on Unsplash

Having a strong and secure password is more crucial than ever in today’s digital world. However, with so many accounts to maintain, it might be difficult to create a unique and complicated password for each one. Python’s Random Password Generator comes in handy here. You can simply generate a secure password that is unique to each of your accounts with our easy tool.

Step-by-Step Explanation of Code

Before we get started, you need make sure Python is installed on your computer. After that, open your favorite text editor and create a new Python file name password_generator.py.

Start by importing the random module, which we’ll use to generate our password. Then, define three lists: one for letters, one for numbers, and one for symbols. These lists will be used to generate our password based on the user’s input.

import random

letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']

Once our lists are set up, we can begin generating the password based on the user’s input. We’ll use the input() function to prompt the user to choose one of four options:

  1. Only letters and numbers
  2. Only letters and symbols
  3. Only numbers and symbols
  4. Letters, numbers, and symbols
print("\nWelcome to my Random Password Generator!")
choice = int(input('There are 4 types of combinations to have in your password.\n1. Type 1 for "Only Letters and Numbers"\n2. Type 2 for "Only Letters and Symbol"\n3. Type 3 for "Only Numbers and Symbol"\n4. Type 4 for "Letters, Numbers and Symbol"\nWhich combination you want? : '))

Based on the user’s choice, we’ll prompt them to enter the number of letters, numbers, and symbols they want to include in their password. Using a for loop and the random.randint() function, we randomly generate a password with the desired number of characters of each type.

User Typed 1 (Only letters and numbers)

if choice == 1:
nr_letters= int(input("\nHow many letters would you like in your password?\n"))
nr_numbers = int(input("\nHow many numbers would you like?\n"))

for n in range(0, nr_letters):
password += letters[random.randint(0, len(letters)-1)]

for n in range(0, nr_numbers):
password += numbers[random.randint(0, len(numbers)-1)]

User Typed 2 (Only letters and symbols)

elif choice == 2:
nr_letters= int(input("\nHow many letters would you like in your password?\n"))
nr_symbols = int(input("\nHow many symbols would you like?\n"))

for n in range(0, nr_letters):
password += letters[random.randint(0, len(letters)-1)]

for n in range(0, nr_symbols):
password += symbols[random.randint(0, len(symbols)-1)]

User Typed 3 (Only numbers and symbols)

elif choice == 3:
nr_numbers = int(input("\nHow many numbers would you like?\n"))
nr_symbols = int(input("\nHow many symbols would you like?\n"))

for n in range(0, nr_numbers):
password += numbers[random.randint(0, len(numbers)-1)]

for n in range(0, nr_symbols):
password += symbols[random.randint(0, len(symbols)-1)]

User Typed 4 (Letters, numbers, and symbols)

elif choice == 4:
nr_letters= int(input("\nHow many letters would you like in your password?\n"))
nr_numbers = int(input("\nHow many numbers would you like?\n"))
nr_symbols = int(input("\nHow many symbols would you like?\n"))

for n in range(0, nr_letters):
password += letters[random.randint(0, len(letters)-1)]

for n in range(0, nr_numbers):
password += numbers[random.randint(0, len(numbers)-1)]

for n in range(0, nr_symbols):
password += symbols[random.randint(0, len(symbols)-1)]

User Typed Except (1–4)

else:
print("\nYou typed a wrong number. Please TRY AGIAN!")
exit()

Finally, we check if the password is at least 8 characters long and shuffle the characters to create a stronger password. If not then we will tell the user to try again for a 8 characters long password.

print(f"\nTotal character of your password is: {len(password)}")
if len(password) >= 8:
password = ''.join(random.sample(password, len(password)))
print(f"Your password is: {password}")
print("Congratulations for generating a STRONG password!")
else:
print("\nIt's highly recommended to have atleast 8 characters in your password to have a stronger password. Please TRY AGIAN!")

Here is the full code.

random_password_generator.py

Test Output

From Left: Password Options, A Test Output

Features

Program’s Features

  • The program allows the user to choose from four types of password combinations.
  • The program asks the user for the number of letters, numbers, and symbols they want in their password.
  • The program generates a strong, random password according to the user’s specifications.
  • The program checks if the password is at least 8 characters long and shuffles the characters to create a stronger password.

Python Concepts and Techniques Used

  • Lists: we define three lists of characters (letters, numbers, and symbols) that can be used in the password.
  • Random module: we import the random module to randomly generate the password.
  • Control flow: we use if-elif-else statements to determine the type of password combination and the number of characters of each type.
  • Loops: we use for loops to randomly generate the password characters.
  • String manipulation: we use the join() function to shuffle the password characters.

In this article, we learnt how to use Python to develop a random password generator. To build a strong, random password based on the user’s parameters, we used lists, the random module, control flow, loops, and string manipulation. Anyone who needs to generate a strong password for their accounts will find this program beneficial.

This article is a part of my Python Project Journal. To learn more about my journey in learning Python and to see my other projects, check out this project journal. Thank you!

--

--