Super Simple Python is a series dedicated to creating super simple Python projects for beginners to be able to do in under 15 minutes. In this episode, we’ll be covering how to build an adjustable Python password generator in under 30 lines of code!
For a video version (that’s actually a little longer in code, I optimized the code for this post):
Like many of the projects we’ve built in the Super Simple Python series like the Random Number Generator, the Dice Roll Simulator, Rock Paper Scissors, and the High Low Guessing Game, the Password Generator relies on the
random library. Our password generator will also use the
string library. These libraries are both native to Python so we don’t have to use
pip to install anything.
We’ll start off by importing these libraries.
import random import string
Defining a Password Generator Function
We’re generating a password so we’ll start off by creating a function that will do that. This function will take five parameters. Four of those parameters will have default values, only one will be required. The
length parameter will dictate the length of our password. The other four parameters,
punctuation will tell the program whether we want to include lowercase, uppercase, digits, and punctuation respectively. We’ll set all of these to
True by default.
We’ll start off with an empty string,
char, that will hold a representation of all the characters we can choose from to create our password. For each of the
punctuation we’ll add those characters to
char if they’re true. The function will then return a string that joins a
length number of randomly chosen characters from the
char string as our password.
def pw_gen(length, lower=True, upper=True, digits=True, punctuation=True): char = "" if lower: char += string.ascii_lowercase if upper: char += string.ascii_uppercase if digits: char += string.digits if punctuation: char += string.punctuation return ''.join(random.choice(char) for _ in range(length))
Allowing the User to Adjust Input
Now that we’ve created a function to generate a random password, we should let the user adjust the password to their needs. Prompt the user for the length of the password they want. We will also if they want to adjust its parameters. If they want to adjust the password parameters, we’ll go through each of the possible parameters and ask if they want to include those characters in their password. We will use
n for yes or no respectively and adjust our
False input values for those parameters to the password generator function we created. Finally, we print a randomly generated password.
_length = int(input("How long of a password do you want? ")) adjust = input("Do you want to adjust the password parameters?(y/n) ") if adjust == "y": _lower = input("Do you want lowercase characters?(y/n) ") _upper = input("Do you want uppercase characters?(y/n) ") _digits = input("Do you want digits?(y/n) ") _punc = input("Do you want punctuation?(y/n) ") lower = True if _lower == "y" else False upper = True if _upper == "y" else False digits = True if _digits == "y" else False punc = True if _punc == "y" else False print(pw_gen(_length, lower, upper, digits, punc)) else: print(pw_gen(_length))
If the user doesn’t want to adjust the parameters of the password generator we should see an output like:
If they do choose to adjust the password generator parameters we’ll see an output like:
I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.DonateDonate monthlyDonate yearly