Random Password Generator – Python

In this tutorial, you’ll learn to create a random password generator with python. This tutorial is dedicated to making you able to generate random passwords of the desired length whenever necessary.

Why this? Where to use it?

These kinds of programs are used when you are trying to make some kind of websites or apps where the user needs to signup with an account and you need to suggest passwords for them, you can use these kinds of password generator scripts to generate random passwords for suggestions.

Also when you change your password for some sites and couldn’t find a much stronger password, you can use this kind of script to generate a strong password for you.

Getting Started with Random Password Generator

To go through this script, you do not need any advanced skill or professionalism in python however a little familiarity with the basics and syntax of python is expected.

Packages Installation

The only package you need for this is random the library of python which is pre-installed with pip. So you don’t need to install any external library for this task.

Algorithm

The steps for the task are as follows:

  1. Import the required libraries.
  2. Then you assign different values like lowercase and uppercase letters, numbers, and special characters to include in the password.
  3. Thereafter, you ask the user for no of the characters the password length should be of.
  4. Then you select any random digits from values in step 2, with the length from step 3, and generate the password.

Diving into the Code

Importing Packages

The first and foremost thing you need to do is import the required packages. As mentioned earlier, for this task you require only one random package and as it’s a pre-installed library in python, you’ll import it directly to your code.

import random

Assigning Values

Now, you need to assign values that will be used while generating passwords. Four kinds of values will be used for this task, lowercase alphabets, uppercase alphabets, numbers, and special characters.

lower='abcdefghijklmnopqrstuvwxyz'
upper='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
numbers='12345678901234567890'
symbols='!"#$%&/()={}[]'

Sum up all assigned values

Since you have assigned all four different kinds of values you’ll be using to generate passwords, now you sum up all these values into a single variable.

all_characters=lower+upper+numbers+symbols

Getting Password Length

Now we’ll get the user input for password length (this is optional and instead of getting user input you can make this a fixed value.)

n_chracters=int(input('Digit Number of Characters: '))

Generate Password

Now, you’ve all the values and desired no of digits to generate the password, so you’ll select the digits randomly and generate a password.

password=''.join(random.sample(all_characters, n_chracters))

Print Password

Now we’ll print out the generated random password.

print(password)

Complete Code

Here is the complete code for this tutorial.

import random
lower='abcdefghijklmnopqrstuvwxyz'
upper='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
numbers='12345678901234567890'
symbols='!"#$%&/()={}[]'
all_characters=lower+upper+numbers+symbols

n_chracters=int(input('Digit Number of Characters: '))
password=''.join(random.sample(all_characters, n_chracters))
print(password)

Conclusion

So, now you’ve learned to generate random passwords using python. Now you can use this script to generate random passwords of a specific length for passwords suggestions on your websites or apps or generate random passwords for yourself.

Posted by Awebisam

I am Awebisam, a tech and economics enthusiast with native roots in Python and JS. Apart from those, TS and Golang are also my type of things.
I work with Django, FastAPI, Vue and Nestjs. Also slightly familiar with Flask, Express, and Svelte.

~ Awebisam, Aashish Bhandari

Leave a Reply

Your email address will not be published. Required fields are marked *