Kodeclik Blog
How to make a password generator in Python
Aren’t you tired of thinking up new passwords for every application or website? Wouldn’t it be nice to create your own personal password generator? In this blogpost we show you how!
What makes a good password?
A good password should be difficult to guess and it should have a mix of different types of characters: letters, numbers, special characters. Further, the letters should be of different cases, ideally. There could be some memorable keywords inside the password but in overall it should not be an obvious choice. For instance “kodeclik” is not a good password choice but “Y!*kodeclik9Bx” could be.
Sample runs
Here are two runs with our Python program that we present below. In the first run we choose to have a password keyphrase (here, “kodeclik”) and in the second we do not have a keyphrase.
Hello, we will be making a random password for you to use today!
Would you like a keyword?: yes
Enter keyword: kodeclik
Here is your own personal password: Y!*kodeclik9Bx
Hello, we will be making a random password for you to use today!
Would you like a keyword?: no
Here is your own personal password: #Y00Q#q!4(^m%!69O
Choosing a single random character
To begin, let us make a function that chooses a single random character for our program. The below code defines a function that returns a random character, which could be a letter (uppercase, lowercase), a number, or a special character. This function (and everything below) makes heavy use of the random package in Python. Here is the full function
import random
def randomcharacter():
randomizer = random.randint(1,3)
if (randomizer == 1):
return random.randint(0,9)
elif (randomizer == 2):
alphabet = "abcdefghijklmnopqrstuvwxyz"
caps = random.randint(1,2)
number = random.randint(0,25)
letter = alphabet[number]
if (caps == 1):
letter = letter.upper()
return letter
else:
specialcharacters = "!@#$%^&)*(|"
number = random.randint(0, 11)
return specialcharacters[number]
To decide what type of character to generate we choose a random number from 1 to 3 (inclusive). We use the random.randint() function for this purpose.
If we obtain 1, we will select a random number; if we obtain 2, we will select a random letter; and if we obtain 3, we will select a random character. To obtain a random number we use the same function, i.e., randint, to obtain a number from zero to nine inclusive.
For the letters, we create a string containing all letters and then select a random integer from 0 to 25 to decide on the index into the string. We also randomize the choice of lowercase or uppercase and transform the character appropriately.
Embedding a keyword in your password
Now we write the main driver routine. The code below tells the user that the computer will generate a random password, and then uses the input function to ask them if they would like a keyword.
For this question, if the user says “yes” we ask them what their keyword is. For example if my favorite animal was a jaguar and I chose jaguar as a keyword, we could use this to make a password like “23#jaguarsrock!”. This password is not the best one though so our program will make the passwords completely random.
The three main things we need to remember are: the greater the length of the password, the higher the security; the password should contain special characters like “#”; and finally it should contain a mix of upper and lowercase letters.
The code below checks if our user said yes to a keyword. If so, we ask for the keyword and then identify in what places we can place our keyword. Assume our password is determined to have fifteen characters. Suppose our keyword is the letter “a”. There are sixteen places we can put this letter in.
Therefore, we pick a random number to identify the location of the keyword. In the for loop to generate the password, we add characters till we reach the location of the keyword, then add the keyword, and then continue to add random characters.
Driver Program
The full driver program is:
print("Hello, we will be making a random password for you to use today!")
ask = input("Would you like a keyword?: ")
while (ask.lower() != "yes" and ask.lower() != "no"):
ask = input("This is a yes or no question: ")
if (ask.lower() == "yes"):
keyword = input("Enter keyword: ")
while (len(keyword) > 10):
print("Keyword can be no more than seven characters long")
keyword = input("Enter keyword: ")
length = random.randint(13, 20)
lengther = len(keyword)
placementcheck = length - lengther
placement = random.randint(0, placementcheck)
password = ""
for i in range (placementcheck + 1):
if (i != placement):
char = str(randomcharacter())
password += char
else:
password += keyword
else:
length = random.randint(13, 20)
password = ""
for i in range (length):
char = str(randomcharacter())
password += char
print("Here is your own personal password:", password)
In this blogpost we have learnt how to generate random passwords. Learn more about Python's random number generation capabilities. Also learn about Python's enumerate() functionality.
Want to learn Python with us? Sign up for 1:1 or small group classes.