Home TechnologyCoding How to Create your Own Password Generator Using Python

How to Create your Own Password Generator Using Python

by Ivan
How to Create Your Own Random Password Generator in Python

Need a secure password? Learn how to code your own random password generator in Python.

Python is an open-source programming language which has found widespread use in various domains. From creating web applications to data management, Python has become the go-to programming language for beginners and advanced users alike.

The programming language is used extensively to secure systems and generate passwords and token systems for enhancing security.

This tutorial will help if you struggle with generating passwords and want an automated password generator at the click of a few buttons.

Why Create a Random Password Generator?

The intent is to create a random password generator using Python, which can help you create a strong password(s) for your system(s).

By following a series of simple, easy-to-execute codes, you can create random passwords using a combination of alphabets and special characters within Python.

The password(s) created will be based on parameters you specify during the coding stage and can include/exclude alphabets, special characters, and numbers, as per your liking.

Requisites for Creating a Random Password Generator

Here are some requisites to create your very own random password generator:

  • Python’s latest version: Python is a user-friendly programming language; you can download the newest version from Python’s website.
  • ​​​​Basic knowledge of Python: While even a novice can create this password generator, it’s often good to have a basic understanding of how Python works and the various interfaces where you can type in code.

How to Set Up Your Random Password Generator

Depending on your comfort level, you can either use Jupyter Notebook to write codes or use the IDLE version.

Continue with the Jupyter interface for this guide.

Import the Random Module

Since you need to generate a set of random passwords/strings, you must import the random module as a part of the first step.

Use the import command to import the random module into your Python’s session:

import random

Specify a Set of Desired Password Characters

Next, you need to create a new variable with the desired alphabets, numbers, and special characters you would like to use in your random password. These can be any series of letters, characters, numbers and special characters. You can add/remove characters as you like.

The string appended below is used as an example of random characters and special symbols.

Chars = “abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789!@#$%^&*()”

When you run this password generator, it will pick up random combinations from the string specified above and create a strong password for you.

Run a Loop to Execute as Per User’s Inputs

Now, you need to run a loop to manage the length of the password. A loop within Python will iterate the code for a certain number of times, until the required criteria is met.

In this case, the loop will ask the user for the desired length of the random password.

Create the first loop using the while command:

while 1:
password_len = int(input("What length would you like your password to be : "))
password_count = int(input("How many passwords would you like : "))


  • input: Input statement will request the user for an input value
  • int: Int will convert the user input value into a numeric (integer) value
  • password_len: New variable to store the length of the password (user entered value)
  • password_count: New variable to store the number of passwords required by the user (user entered value)

Define loop parameters from starting to the ending point. Remember, the starting point will be 0, while the user will define the ending point. This is dependent on the value entered by the user during the prompt in password_count.

 for x in range(0, password_count):
 password = ""


  • x = x is a counter
  • range = Range will capture the starting and ending values entered by the user
  • password = password variable created with a blank string placeholder

Create a Password Generating Loop

To create a password generating loop, you need to open a loop using another for statement.

 for x in range(0, password_len):
password_char = random.choice(Chars)


  • x: x is a counter variable
  • range: Range will capture the starting and ending values entered by the user
  • password_char: New variable to generate a random set of characters from the aforementioned string char
  • random: This is a pre-stored module available within Python, which you imported in the first step (the import statement)
  • choice: As the name suggests, it will choose a single character from the defined variable/values. In this case, it would choose a single value from the chars variable value whenever the loop is executed.

Concatenate the Random Value With the Default Password Value

So far, you have generated a default password (blank value) and picked up random characters using a loop. Random characters need to be strung together to form a cohesive password, which you can use. To do so, you need to start concatenating the character values together.

 password = password + password_char
print("Here is your random password : " , password)


  • password: This will combine all the random values picked up by the random and choice statements.
  • print: Print statement will print out the results in an easy-to-understand manner

The final program will look like this:

import random
Chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789!@#$%^&*()"
while 1:
password_len = int(input("What length would you like your password to be : "))
password_count = int(input("How many passwords would you like : "))
for x in range(0, password_count):
password = ""
for x in range(0, password_len):
password_char = random.choice(Chars)
password = password + password_char
print("Here is your random password : " , password)

When the code runs, it will ask the user the length of the passwords in the first iteration. As soon as you update the value, press Enter.

In the second iteration, you would be asked to enter how many passwords you would like Python to generate for you. Input the value and press Enter.

In the final iteration, Python’s password generator will generate random passwords based on the length and the number of passwords specified.

Note: Make sure you keep the indentation as shown in the code above to avoid indentation errors. Also, Python is very particular about upper and lower-case functions, so be careful how you define the syntax statements.

Generating Your Random Password in Python

This program will allow you to print as many passwords as you want. Just feed in the total length of the passwords and the number of passwords you need.

The code is straightforward to follow, and each step is executed step-by-step so that Python can run the program basis the user’s inputs.

You may also like

Leave a Comment