In this post, we will create a simple password generator in Python by taking input from the user with a detailed explanation and example.

If you are a beginner, you can consider this as your mini project where you randomly generate passwords. Additionally, this program will help you grasp your logic. So let’s start learning and making password a generator in Python.

But before we jump into the algorithms or programming part few programming concepts you must know:

Algorithm

  1. Import random module
  2. Create three lists, The first one contains a list of alphabets (upper or lowercase both), the second list contains a list of numbers from 0 to 9 and the third one contains a list of special characters such as @, #, $, ^, &, *, etc
  3. Take user input such as how many characters, numbers, and special characters they want in their password.
  4. Create an empty list to store your password (password_list = []).
  5. Using for-loop iterate over user input times in each list and randomly choose characters, numbers, and special characters and append it in empty list password_list
  6. After that join your password_list list into the string using the join() method.
  7. print your password.

From the above algorithms, now we know how we will implement a password generator in Python. So let’s start writing code for it.

Source Code

import random

alphabete = ['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']

number =[0,1,2,3,4,5,6,7,8,9]

special_charecter = ['!','@','#','$','%','^','&','*','(',')','_','-','+','=']


char_in_password = int(input('How many alphabate you want in your password : '))
number_in_password = int(input('How many number you want in your password : '))
special_char = int(input('How many special charecter you want : '))

password_list = []

for j in range(1,char_in_password+1):
    ch = random.choice(alphabete)
    password_list.append(ch)
for k in range(1,number_in_password+1):
     num = str(random.choice(number)) # choice method choose random item from list
     password_list.append(num)
for l in range(1,special_char+1):
    sp = random.choice(special_charecter)
    password_list.append(sp)
    
random.shuffle(password_list)
password = ''.join(password_list)
print(f'your password is [ {password} ]')

Output-1

How many alphabate you want in your password : 4
How many number you want in your password : 3
How many special charecter you want : 1
your password is [ H2HF6@3e ]

Output-2

How many alphabate you want in your password : 5
How many number you want in your password : 3
How many special charecter you want : 1
your password is [ V(A09eJZ2 ]
Author

Hi, I'm Yagyavendra Tiwari, a computer engineer with a strong passion for programming. I'm excited to share my programming knowledge with everyone here and help educate others in this field.

Write A Comment