How to Create Your Own Random Password Generator in Python

Need a secure password? Take matters into your own hands and 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:

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.

RELATED: How Does The Python String Format() Method Work?

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 : "))

Where:

RELATED: How To Use A While Loop In Python

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 = ""

Where:

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)

Where:

RELATED: How To Use For Loops In Python

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)

Where

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)

Output:

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 through, and each step is executed step-by-step so that Python can run the program basis the user’s inputs.

Exit mobile version