DEV Community

Spandan
Spandan

Posted on • Updated on

Python Script to make a Password Generator

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

Top comments (4)

Collapse
 
wjplatformer profile image
Wj

It is a good practice to put variable names that store passwords to be.... weird. Such as "BlueSkyeOnName" For password. This makes it harder for hackers to access stuff, especially if you are building a network with limited cybersecurity knowledge.

Collapse
 
spandan profile image
Spandan

Ok thanks for the information I'll take care of it next time. ๐Ÿ™‚

Collapse
 
morphzg profile image
MorphZG

Thanks for sharing. I am also learning python and find this exercise online. I did it a bit different but i like your approach also. You could let the user choose strenght also. Weak password with only letters, medium with added digits and strong with all printable characters.

Collapse
 
spandan profile image
Spandan

Thanks for your reply stay tuned for more content .
I am glad to hear that you liked it .