Beginners Guide to Password Generator using Python

Atulya Khatri 24 Nov, 2021 • 5 min read

This article was published as a part of the Data Science Blogathon.


  • What is a “password generator using python”?
  • What exactly is a password?
  • How to make it?

What is “password generator using python”?

When you are filling some passwords on sites like you are signing in on Analytics Vidhya through chrome, you will get a suggestion from chrome about the password; it will recommend you a password, though you can use it or just create your own. The same thing you can create your own and I am here to help you to create it. So we are going to create it using python and its libraries or modules.

Before we make it, we should know what exactly is a password.

What exactly is a Password?

As we know, the password is a key in the digital world or even in the real world. So what exactly a password is, a set of characters containing letters of both upper case and lower case, digits, and punctuations.

Before creating the password we should know that the suggested length of the password is more than 4 characters and less than 16 characters. Since in the case of less than 4 character passwords, it is much easier to be decoded and in the case of more than 16, it is very hard to decode or simply for security reasons.

Here also we are making our length of the password to be between 8 and 16 characters.

Password generator cover

How to make a password generator?

We are going to make it using python and its libraries. So libraries used here are following

  • string – To work with lower case and upper case alphabets, digits, and punctuations
  • random – To reorder characters

Since both these modules come built-in in python, so we don’t need to download any

First, we are going to import the modules.

import string
import random

Now we make a function named password().

def password(): #creating a password generating function 

    """This is a password generator"""

    store = [] #It will contain passwords

    char_length = random.randrange(2,5)

    for i in range(char_length):# using string module to generate the password

        cap = random.choice(string.ascii_uppercase)

        store += cap

        small = random.choice(string.ascii_lowercase)

        store += small

        digit = random.choice(string.digits)

        store += digit

        punct = random.choice(string.punctuation)

        store += punct

    random.shuffle(store)# Shuffling it

    random.shuffle(store)# Shuffling it again 

    word = "".join(store) #Making the password a string

    return word

As said above, we have made a function named password(), and then below we have written docstring of this function. NOTE: YOU CAN GIVE A DOCSTRING OF YOUR FUNCTION BY WRITING THEM BETWEEN THREE QUOTATIONS MARKS ON BOTH SIDES. IT IS USED TO GET TO KNOW ABOUT WHAT EXACTLY A FUNCTION IS DOING.

Now back to the point, now we have created a “store” variable that will contain the characters of the password. Now the main game starts as we are going to create the password.

By making a variable “char_length” in which a function from the random module that is random.randrange() is used to get a number between given ranges. Here the ranges are 2 to 4 by which we will get 8 to 16 characters of the password.

TIP: You can change the range for your sake. Then we are using for loop to get every character and then shuffle it.

Then we have created a “cap” variable to get an uppercase letter. In this variable, we have used random.choice(string.ascii_uppercase) in which “string.ascii_uppercase” is used to get the ASCII characters or simply to understand: All the writable characters that are present on your keyboard are ASCII characters like “a,q,3,$,’,+” and not ‘up arrow key or shift key’. So we have imported ascii_uppercase here means uppercase characters from the string module and then using random.choice() function we have selected a single character from all the uppercase characters.

Then we add it to the “store” variable using “+=” like “store += cap”.

As we did to get an uppercase letter, the same we can do to get a lowercase letter like creating a “small” variable that will contain a lower-case letter. To do this we have used random.choice(string.ascii_lowercase), same as uppercase we have got a lower case letter from string module and then using “choice” function from the random module we have selected any lowercase letter from all the given lower case letters.

And added this letter to store the variable in the next line using “store += small”.

Again we are doing this to get a digit, that is we have created a “digit” variable that contains the digit in string format. To do so we have used “random.choice(string.ascii_digits)” in which we will get ASCII digits from the string module and then select a digit from the choice function of the random module.

Then we added this character to the “store” variable.

At last, we have to get punctuation characters for our password which we will get by creating a variable named “punct” which will contain the punctuation characters. To do so, we used the string module to get ascii_punctuation and then used a choice function from random module like “random.choice(string.ascii_punctuation)” to select any of these characters.

Then we added the character to the store variable like “store += punct”.

This loop will run between 2 to 4 as given in the “char_length” variable and so characters will be of  8 or 12 or16 characters. Since in one loop password will be of 4 characters, then as the loop increases the character length will increase.

Now we will shuffle the store variable to rearrange the characters in it and again we will shuffle it to make it unpredictable.

Then we created a “word ” variable in which the list characters will be converted to strings or simply a password. To do so we have used the built-in function “join” to convert “store” variable containing the password in list format to a password in string format. At last, we will return the password that is in variable word.

Now we will end this like:

if __name__ == "__main__":

As you know in python “__name__” function is used to get the module anywhere just by writing import and then the name of the module.

In the end, we have called password() function and printed it on the screen using print() function like print(password()) and finally get our password.

screenshot of prompt

Password generator program screenshot

Above is a snippet of the program we have made and you can see, we have got different lengths of our password every time we run the program.

Voila! You have made your own password generator like chrome’s password generator.


This is how you make your password generator.

Now some points to know when you are using this password generator.

As you know remembering a password is not an easy task and if you are using passwords like this which contain lots of punctuations, digits then I don’t think so that you can remember it for long. As we use passwords that are something memorable or some names or something easy to remember.

But you can use this like chrome’s password generator that is when you are creating a password then use this program to generate a password and put it there and then let it save in chrome’s password manager.


I am Atulya Khatri a python geek. I learn python and teach it in my local.
You can also check out my previous blog on Analytics Vidhya which is how to make an infinite timer using python.

Cover page photo link:
Photo by Wiredsmart from Pexels

In-site photo link:

If you face any problem with the program then feel free to ask in the comment section.

Do share this with all your friends who you think need this.

The media shown in this article is not owned by Analytics Vidhya and are used at the Author’s discretion

Atulya Khatri 24 Nov 2021

Frequently Asked Questions

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

Responses From Readers


Related Courses
0 Hrs 13 Lessons

Introduction to Web Scraping using Python


  • [tta_listen_btn class="listen"]