How to make a password generator in Python

GoodMorningMind
3 min readJan 4, 2021

How’s it going folks!

Today I’ll teach you how to make a py script to generate your own strong passwords. It’s quite useful if you don’t trust password generators in browsers or password generator apps.

Don’t worry! It’s very simple, just follow my steps, I’ll explain everything step by step!

Requirements:

  1. Python IDLE (https://www.python.org/downloads/);
    Note, you can use any Python dev. environment, like PyCharm or Spyder. I personally like to use IDLE.

And that’s all!

Install and fire it up! Once you open IDLE press CTRL + N.

This window will open and now we start writing the scrip!

Type this into your editor:
import string
from random import *

“import string” is a built in module which we have to import to use and “from random import*” imports the random library.

This part of the script defines what will be used to generate the password.
In this case what will be used are ASCII letters, punctuation and digits.
And the second line of code combines all those characters into one.
The “in range(randint(18, 24)))” is where we choose the length of the password, in this example its 18 to 24, but you can modify that to your liking, if it’s for a site that does not allow more than 16 characters then modify it to be, for example “(14, 16)))”. Of course, you can also put in a higher amount of characters, like 64 if you want a really really strong password, for crypto wallets for example.

And the final bit of code!
The role of this is to display a message that the password has been generated and the generated password after it. You can modify the text to say whatever you want, “Generated a password”, “Secure password generated”, whatever you want. You could also add one more line like this:

This is if you want to add one more line that says who it’s been made by.
It’s completely optional and it’s just if you want to brag a bit, a humble brag really. You can write “Made by YourName”, “This has been made by YourName” and so on, you get the point.

Now save the script and press F5 to run it.
This will be the output:

And that’s it! Your very own password generator!
That wasn’t so hard, right?

Soon there will be a video tutorial of this, and many other python and IT tips and tricks on my YouTube channel so subscribe if you’d like to learn more!
LINK TO MY CHANNEL

--

--