Confused about Password choice? Build a Random Password Generator

Confused about Password choice? Build a Random Password Generator

Hello everyone,

After reading this blog article, you will never feel confused about your Choice of Password . We will see the implementation through Python.

Let's get started!

Passwords provide the first line of defense against unauthorized access to your computer and personal information. The stronger your password, the more protected your computer will be from hackers and malicious software. You should maintain strong passwords for all accounts.

Module Used:

Random Module:

This module generates pseudo-random numbers. Here we are going to use random.sample() as it doesn't produce repetitive elements while generating passwords. To know more about this module, visit Random Module docs

String Module:

The string module contains a number of functions to process standard Python strings. Here we'll use few:

  • string.ascii_letters: Performs concatenation of upper and lowercase letters.
  • string.ascii_uppercase: Produces upper case letters.
  • string.ascii_lowercase: Produces lower case letters.
  • string.digits: Produces string comprising of 0123456789.
  • string.punctuation: Produces string of ASCII characters which are considered punctuation characters in the C locale: !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~.

Let's start Coding

Whole Code Snippet

import random
import string

print("Welcome to Password Generator!")

length = int(input('\nEnter the length of the Password: '))
lower = string.ascii_lowercase
upper = string.ascii_uppercase
num = string.digits
symbols = string.punctuation

all = lower + upper + num + symbols

temp = random.sample(all, length) 
password = "".join(temp)
print(password)

Step by Step Guide:

-> Import the packages in your python script. Use the command below:

import random
import string

-> Now printing a Welcome message

print("Welcome to Password Generator!")

-> User input command for the length of the Password we want to get

length = int(input('\nEnter the length of the Password: '))

->Now, let's define the data by storing lowercase letters, uppercase letters, numbers and symbols.

lower = string.ascii_lowercase
upper = string.ascii_uppercase
num = string.digits
symbols = string.punctuation

-> It's time to combine the data. Then storing the data in all variable.

all = lower + upper + num + symbols

->Let's make use of random module to generate the password.

temp = random.sample(all, length)   #Passing two Parameters
password = "".join(temp)

-> Print to get the password

print(password)

YAY! A fresh password got generated.

See you in my next blog. Take Care!