Create Random Password Generator Program Using Python

Shreya on July 15, 2023

Create Random Password Generator Program Using Python

Introduction

In today’s digital world, it’s important to have strong and secure passwords for all our online accounts. But creating unique and complex passwords can be pretty challenging, especially for kids. Don’t worry, though!

In this article, we will walk you through creating a fun and interactive password generator program using Python. By the end, you’ll have a tool to generate strong passwords for all your online adventures. Let’s get started!

What is a Password Generator?

A password generator is a program that creates random and strong passwords for various online accounts. It eliminates the need for using predictable and easy-to-guess passwords like “123456” or “password.”

A strong password consists of at least more than ten characters with a combination of characters such as percent (%), commas(,), and parentheses, as well as lower-case and upper-case alphabets and numbers.

With a password generator, you can create unique and secure passwords that are difficult for others to guess, protecting your online accounts from unauthorized access.

Generate a Random Password Maker

You must be excited to develop your random password generator in Python. It is recommended to have a good understanding of Python programming, like the working of loops, operators, and functions. Along with that, we will be using some data structures like lists, strings, etc. Don’t stress if you don’t have any prior knowledge of Python; we have got you covered. Take this elaborate and interactive course by Codingal and become a Python Guru!

Importing Python Libraries

First thing first, As a first step, let’s import the “secrets”, “string”, and “random” modules. This module is built into the Python standard library, so you can import it as follows:

import secrets
import string
Import random

These modules in Python provide us with various functions that help us to randomly choose among various values.

Password Selection List

The next step is to define the password selection list. This denotes the set of all characters we’ll consider for password generation. We want it to contain the following: lowercase and uppercase letters, numbers, and any special characters.

If you want to add something extra to your password selection list, you would have to define it explicitly.

The “string” module provides string constants that we can use to define the alphabet.
• The “ascii_letters” is a concatenation of lowercase letters and uppercase letters.
• The “digits” constant is the string containing the numbers 0 to 9: ‘0123456789’.
• The “punctuation” constant is the string of all special characters.

Now let’s write a small code snippet to include this component of the “string” module.

letters = string.ascii_letters
digits = string.digits
special_chars = string.punctuation
selection_list = letters + digits + special_chars

Finally, in the last line, we have concatenated the above string constants to get the selection list.

Password Length

Now, our job is to select the length of the password. You can choose any length, but in this example, I have used the length to be 10.

password_len = 10

I have initialized a variable “password_len” with 10.

For Loop

Our next job is the most interesting one. We need to write the code to generate a password of length 10 using a for loop. The basic logic behind this is to select 10 characters separately from the password selection list defined above.

password = ''
for i in range(password_len):
password+= ''.join(secrets.choice(selection_list))

print(password)

Let’s discuss the above code snippet and what function it performs:
• The “password” string is an empty string initially.
• We know that secrets.choice (selection_list) returns one character, chosen at random from the alphabet.
• We use the “join()” method to add this character to the “password” string.
• Because we do not want any whitespace between the characters, we specify the separator as a single quotation mark ‘ ‘.
• To repeat the above steps to generate a password, we use a loop that runs for 10 iterations as the length of the password.
Kudos on creating your very own random password generator in Python!

The complete code is given below.

Code

Your code should look something like this:

import secrets
import string
Import random

letters = string.ascii_letters

digits = string.digits

special_chars = string.punctuation

selection_list = letters + digits + special_chars

password_len = 10

password = ''
for i in range(password_len):
password+= ''.join(secrets.choice(selection_list))

print(password)

Every time you run the above code snippet, you will get a new random password. Some sample outputs might be:

}2:Lz,1!Gv
!1jIu.87yt
Rx+z13#.2o

Customizing Passwords Based on Constraints

You have successfully created a password generator. How about we add some interesting constraints to our password generator to make it more personalised and secure?

You might want to limit the use of special characters or the use of each character in a password only once. You can apply all these constraints using conditions and a loop.

For example, I have put a constraint that the password generator should include at least 1 special character and at least 2 numbers.

import secrets
import string
Import random

letters = string.ascii_letters

digits = string.digits

special_chars = string.punctuation

selection_list = letters + digits + special_chars

password_len = 10

while True:
password = ''
for i in range(password_len):
password += ''.join(secrets.choice(selection_list))

if (any(char in special_chars for char in password) and 
sum(char in digits for char in password)>=2):
Break 
print(password)

You should know that if the conditions are met (at least one special character and at least two digits), the loop is exited using the break statement.

Finally, the generated password is printed to the console using print(password).

Conclusion

Congratulation! Today you have put your basic concepts of Python programming to generate a random password program. We know that after reading this article, you will experiment with more programs like this one throughout your Python programming.

You can take the time to master the fundamentals of Python by taking our fabulous Python courses. Let’s learn and program together!

Best Coding Course for K12 Kids

Sign up to our newsletter

Get the latest blogs, articles, and updates delivered straight to your inbox.

Share with your friends

Try a free class