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
string : We will use this module to add different characters to our password so it becomes strong and difficult to crack.
random : We will be using this module to pick random characters .
Let's Code
Start by importing the modules
import random
import string
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
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))
Now finally we will generate the password and print it
print("Your password is: ")
print("".join(random.sample(s, pwdlen)))
a = input("")
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)
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.
Ok thanks for the information I'll take care of it next time. 🙂
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.
Thanks for your reply stay tuned for more content .
I am glad to hear that you liked it .