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.

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
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:
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.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

Copyright @ Kodeclik 2024. All rights reserved.