visit
Hi there, let's create a Password Generator Python Project which is super quick & super fun!
Here's an example:
Input:Enter the length of the password: 7
5:$v9<,
So here, when we ran the code we were asked for the size of the password to be generated. We entered the 7 as the input. And we got a random 7-digit password as output.
You can also see that our password consists of numbers, alphabets & symbols which are highly recommended to be used in a password to make it stronger and difficult to guess & brute force.Alright now since you know how our password generator is going to work, now let's get into coding.Alright, so the very first thing we do is to import the required modules for the project. In this case, since we need to create random strings as mentioned before, we are going to make use of the random module. So let's quickly import it.
import random
pass_len = int(input("Enter the length of the password: "))
Here we go! We are going to store the input in **pass_len** variable. Also notice that we are making use of int() function. That's because the input we get from the user will be in string datatype but to make use of this input we first need to convert it into an int datatype. int() will convert our input from string to int.
Now let's move to the next step.pass_data = "qwertyuiopasdfgjklzxcvbnm1234567890[];',./!@#$%^&*()_+:<>?"
password = "".join(random.sample(pass_data, pass_len))
Here on this final step, we have used the join() function which will join our generated password to an empty string on the left.
Within join(), we have random.sample() function which does the main job of generating a password.
random.sample() takes in our pass_data variable which consists of our raw password characters and the pass_len variable which is the user input from the user regarding the length of the password. Basically what it will do is that it will take random characters from pass_data variable of length pass_len.
Now finally let's print out the final output!print(password)