Create Strong and Secure Passwords with Python Password Generator: A Comprehensive Guide
What is a password generator?
A password generator is a program that generates a strong password according to your need. You need to pass some values like the length of the password, the number of upper_case characters, the number of numeric characters, and the number of symbolic characters. Then the program will generate a random password that fulfills your need.
So today through this blog we will build this kind of password generator program in python. And this is a very beginner-friendly tutorial. You need to know basic python knowledge to follow this tutorial. I am not using the function here, so if you don't know the function, then there is no problem to follow.
Demo of our project in replit
Let's start coding our password generator
Step-1: Create a flow chart
Creating a flow chart or mind map (for large projects) is always beneficial. And it is a good habit to have as a developer. So now in step-1 let's create a flow chart with a blank paper in pen or you can use any web tool ( I will prefer it). I am using https://app.diagrams.net/ but you can use anything. [These are free tools]
Step-2: Create some important variables
Now in step-2 after creating a python file (.py) define some variables as a list of different types of characters like -> lower case, upper case, numeric and symbolic. Because I will be choosing my password from these lists.
# Defining some variables
lower_chars = ["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"]
upper_chars = ["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 = ["!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", "-", "=", "[", "]", "{", "}", "<", ">", "?", "/"]
Step-3: Taking inputs
Now start an infinite loop (while True: ) as we provide the facility of generating passwords multiple times from the single-time program running. And then we need to take important inputs like password length, number of uppercase, numeric and symbolic characters from the user as integers. Then we also need to validate them as a certain value and we need to maintain the sum of numbers of uppercase, numeric and symbolic characters should be less than or equal to the password length. If any validation failed then we will break the loop and throw back the user from our program with feedback.
# Input taking
password_length = int(input("What should your password length ? [6 to 14] "))
if password_length < 6 or password_length > 14:
print("Password length should be between 6 and 14")
break
# Take the local var of passwd len
local_passwd_len = 0
# upper case chars
number_of_upper_char = int(input("How many upper characters you want in your password ? "))
local_passwd_len += number_of_upper_char
if local_passwd_len > password_length or number_of_upper_char < 0:
print("Choose right number of the input !!!!!")
break
# numeric chars
number_of_numeric_char = int(input("How many numeric characters you want in you password ? "))
local_passwd_len += number_of_numeric_char
if local_passwd_len > password_length or number_of_numeric_char < 0:
print("Choose right number of the input !!!!!")
break
# symbol
number_of_symbolic_char = int(input("How many symbolic characters you want in you password ? "))
local_passwd_len += number_of_symbolic_char
if local_passwd_len > password_length or number_of_symbolic_char < 0:
print("Choose right number of the input !!!!!")
break
Step-4: Password generation
In this last step, we need to apply the main logic for password generation. First of all, create an empty string as a password. Then calculate the number of lowercase characters we can apply to our password. So now we have 4 main values -> the number of lower characters, upper characters, numeric characters, and symbolic characters. Then we need to generate four lists by using those numbers and four lists of separate characters from the first step.
This means that there need to import the random module and then use random. choices method. The random module is an inbuilt module and the method of this choice is used to pick any given number of random elements from any list.
Always add your import statements at the top of your file
import random
Then we need to create 4 lists -> lower case characters list which has required the number of randomly chosen lower case characters from the list of lower characters at the first step. And now you may have got the idea of the rest of the 3 lists.
# password generation
password = ''
number_of_lower_char = password_length - (number_of_upper_char + number_of_numeric_char + number_of_symbolic_char)
lower_chars_list = random.choices(lower_chars, k=number_of_lower_char)
upper_chars_list = random.choices(upper_chars, k=number_of_upper_char)
numeric_chars_list = random.choices(numbers, k=number_of_numeric_char)
symbolic_chars_list =random.choices(symbols,k=number_of_symbolic_char)
Then, add these 4 lists and create a new list called passwd_chars. Now, shuffle this list to rearrange the elements of the passwd_chars list to give more strength to our password. And finally, loop through the list and add the elements to our empty password string. And wallah our password is ready as per the user's need.
passwd_chars = lower_chars_list + upper_chars_list +numeric_chars_list + symbolic_chars_list
random.shuffle(passwd_chars)
for char in passwd_chars:
password += char
print("Your password is ", password)
Now we can take user input for if they want to generate a new password again or not. If they want to generate a password again we continue the loop if not then we would break the loop. For the user's good experience, we can clear the terminal by importing 'os' module and using os.system() command. Pass 'cls' in the system command if you are in the Windows operating system, or pass 'clear' in the system command if you are in the Linux or MAC operating system.
isEnd = input("Do you want to generate another password? [y/n] ")
if isEnd.lower() == "n":
break
# Clearing the Screen
os.system('clear')
And now it is done, enjoy the password generator program and test it.
Full code
import random
import os
# Password Generator
# Defining some variables
lower_chars = ["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"]
upper_chars = ["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 = ["!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", "-", "=", "[", "]", "{", "}", "<", ">", "?", "/"]
while True:
# Input taking
password_length = int(input("What should your password length ? [6 to 14] "))
if password_length < 6 or password_length > 14:
print("Password length should be between 6 and 14")
break
# Take the local var of passwd len
local_passwd_len = 0
# upper case chars
number_of_upper_char = int(input("How many upper characters you want in your password ? "))
local_passwd_len += number_of_upper_char
if local_passwd_len > password_length or number_of_upper_char < 0:
print("Choose right number of the input !!!!!")
break
# numeric chars
number_of_numeric_char = int(input("How many numeric characters you want in you password ? "))
local_passwd_len += number_of_numeric_char
if local_passwd_len > password_length or number_of_numeric_char < 0:
print("Choose right number of the input !!!!!")
break
# symbol
number_of_symbolic_char = int(input("How many symbolic characters you want in you password ? "))
local_passwd_len += number_of_symbolic_char
if local_passwd_len > password_length or number_of_symbolic_char < 0:
print("Choose right number of the input !!!!!")
break
# password generation
password = ''
number_of_lower_char = password_length - (number_of_upper_char + number_of_numeric_char + number_of_symbolic_char)
lower_chars_list = random.choices(lower_chars, k=number_of_lower_char)
upper_chars_list = random.choices(upper_chars, k=number_of_upper_char)
numeric_chars_list = random.choices(numbers, k=number_of_numeric_char)
symbolic_chars_list = random.choices(symbols, k=number_of_symbolic_char)
passwd_chars = lower_chars_list + upper_chars_list + numeric_chars_list + symbolic_chars_list
random.shuffle(passwd_chars)
for char in passwd_chars:
password += char
print("Your password is ", password)
isEnd = input("Do you want to generate another password? [y/n] ")
if isEnd.lower() == "n":
break
# Clearing the Screen
os.system('clear')
Make sure you give the indentation properly if you want to fully copy the code.
Subscribe to my newsletter
Read articles from Udit Kundu directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Udit Kundu
Udit Kundu
Hello folks, I am Udit a student of class 12 and soon will go to college. I know front-end development in React and a little bit of back-end development in node js. Currently, I am learning Data Science and ML and also exploring React Native. And here in Hashnode, I will share my knowledge in those fields of coding.