DEV Community

loading...
Cover image for How to build a Password Generator using Python

How to build a Password Generator using Python

mindninjax profile image Rishabh Singh ⚡ ・3 min read

Hi there, let's create a Password Generator Python Project which is super quick & super fun!

Alt Text

How do Password Generators work?

Password Generators are nothing but simple programs which are capable of randomly creating strings which consist of alphabets, numbers & symbols.

The Password Generator we are gonna make will take the length of the password as an input and will generate a random password of the same size.

Here's an example:

Input:

Enter the length of the password: 7
Enter fullscreen mode Exit fullscreen mode

Output:

5:$v9<,
Enter fullscreen mode Exit fullscreen mode

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.

Let's Code

Alright, so the very first thing we do is to import 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 random module. So let's quickly import it.

import random
Enter fullscreen mode Exit fullscreen mode

Now let's get the input for the length of the password to be generated.

pass_len = int(input("Enter the length of the password: "))
Enter fullscreen mode Exit fullscreen mode

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[];',./!@#$%^&*()_+:<>?"
Enter fullscreen mode Exit fullscreen mode

Here we have simply defined a string which consists of all the alphabets, numbers & symbols which will be used to generate the password. You can customize this variable as per your wish. If you want to create a password consisting only of alphabets then you can get rid of symbols and numbers from this variable and the resulting password will be consisting only of alphabets.

Let's Finish it!!!

password = "".join(random.sample(pass_data, pass_len))
Enter fullscreen mode Exit fullscreen mode

Here on this final step, we have used 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)
Enter fullscreen mode Exit fullscreen mode

Source Code

You can find the complete source code of this project here -

mindninjaX/Python-Projects-for-Beginners

Support

Thank you so much for reading! I hope you found this beginner project useful.

If you like my work please consider Buying me a Coffee so that I can bring more projects, more articles for you.

https://dev-to-uploads.s3.amazonaws.com/i/5irx7eny4412etlwnc64.png

Also if you have any questions or doubts feel free to contact me on Twitter, LinkedIn & GitHub. Or you can also post a comment/discussion & I will try my best to help you :D

Discussion (6)

pic
Editor guide
Collapse
sqlrob profile image
Robert Myers

You might want to change the guts of how the password is generated.

From the python docs:

Warning

The pseudo-random generators of this module should not be used for security >purposes. For security or cryptographic uses, see the secrets module.

Collapse
mindninjax profile image
Rishabh Singh ⚡ Author

Hello Robert!
We are mainly focused on creating a simple project for beginners to help them fluent in programming & become more familiar with basic problem-solving skills. This project may not be used in a real-life scenario and may only be applied as a training project.

Collapse
sqlrob profile image
Robert Myers • Edited

I understand that, but way too often code from these projects end up in production, or people come away with the idea "random" is good enough.

And considering the change is just changing random.sample to secrets.choice, it's easy enough to start people with good habits.

Thread Thread
mindninjax profile image
Rishabh Singh ⚡ Author

You are right Robert! I will write to my editor and ask him to make changes ASAP! Once again thanks for your suggestion!

People like you help us refine our code and help the community!

Have a great day Robert!

Collapse
wearypossum4770 profile image
Stephen Smith

Good job on the script. Are there any plans to include options such as ambiguous character exclusion?

Collapse
mindninjax profile image
Rishabh Singh ⚡ Author

No, our projects are mainly for beginners so we don't have any plans for adding new functionality to keep it simple.
But thanks for the suggestion, Stephen. I will talk about this with our editor.