Python Script to make a Password Generator

Spandan Sehgal - Nov 16 '21 - - Dev Community

Intro

Passwords are most important thing in our online security and the passwords must be strong so that our info can't get leaked.

So in this post, which is my first one I am going to tell you how to make a password generator in python .

Required Modules

For this python project we will require only two built-in python modules that are :-

  • string
  • random

Use of the modules

  1. string : We will use this module to add different characters to our password so it becomes strong and difficult to crack.

  2. random : We will be using this module to pick random characters .

Let's Code

Start by importing the modules

import random
import string
Enter fullscreen mode Exit fullscreen mode

Now we are going to create our variables which will use string module to store all characters

str1 = string.ascii_lowercase #This will store all the lower case alphabets

str2 = string.ascii_uppercase This will store all the upper case alphabets

str3 = string.digits This will store all the digits

str4 = string.punctuation This will store all the punctuation marks which will make our password more strong

Enter fullscreen mode Exit fullscreen mode

Now we will ask user for how much length the password should be and make an empty list which will join our variables in which all characters are stored

pwdlen = int(input("Enter password length\n"))
s = []
s.extend(list(str1))
s.extend(list(str2))
s.extend(list(str3))
s.extend(list(str4))
Enter fullscreen mode Exit fullscreen mode

Now finally we will generate the password and print it

print("Your password is: ")
print("".join(random.sample(s, pwdlen)))
a = input("")
Enter fullscreen mode Exit fullscreen mode

You can access this code at my GitHub Repo
I hope you all liked this post if you have any issue feel free to ask in the comments section

You can also visit my website for more content

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .