How to Create a Random Password Generator using Python

John Fallin
7 Min Read
How to Create a Random Password Generator using Python

This post will demonstrate how to use Python to create a random password generator.

How to Create a Random Password Generator using Python

A user can demonstrate their authorization to use a device by using passwords. It is crucial that passwords be lengthy and intricate. It should have a minimum of ten characters, including both lower- and upper-case alphabets, numbers, and symbols like percent (%), commas (,), and parentheses. Here, we’ll use Python code to generate a random password.

Create a Random Password Generator using Python

Examples of weak and strong passwords

Example of a weak password : password123

Example of a strong password : &gj5hj&*178a1

 

Modules needed

string: Used to retrieve string constants. Those that we would require are –

string.ascii_letters: ASCII is a system for digital character representation; each ASCII character has a distinct code. All of the ASCII letters, from A to Z and from a to z, are contained in the string constant string.ascii_letters. It is simply the concatenation of ascii_uppercase and ascii_lowercase, and its value is not dependent on locale. As a result, it gives us the entire set of letters as a string that we can use however we please.

string.digits: This pre-initialized string has all nine digits (that is, 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9) in the Arabic numeral system. Remember that the type is still a string constant even though these are digits, and that all of the digits are concatenated like this: “0123456789.” Slicing can be used to access specific numbers if necessary.

string.punctuation: Python offers us all the special characters in a pre-initialized string constant in addition to letters and numbers. These consist of different types of braces, arithmetical, logical, and comparison operators, as well as punctuation such as question, inverted, and comma marks, periods, and exclamation points. It reads as follows:!”#$%&'()*+, -./:;<=>?@[\]^_}{|}~

random: A user can create pseudo-random numbers with the aid of the Python random module. Numerous functions within the module simply rely on the “random()” function. This function yields a decimal number that is strictly less than one and greater than or equal to zero, or a random float that is generated uniformly in the semi-open range [0.0, 1.0). This number is utilized by other functions in different ways. Sequences, integers, and bytes can all be handled by these functions. Sequences are of interest to us for this task. Certain functions, like random.choices, accept a sequence as an input and return a random element selected at random from the sequence.

Code Implementation 

Take the password’s length as input first. Next, we can show a prompt asking the user to select from a list of characters for their password.

Add string.ascii_letters to the character list in order to include letters from the character set.

Add string.digits to the character list in order to include letters in the character set.

Add string.punctuation to the character list in order to include letters in the character set.

 

Use a for loop to iterate through the password length, selecting a random character using random.choice() from characterList for each iteration. Print the password at the end.

Implementation1

Python3

import string
import random

# Getting password length
length = int(input("Enter password length: "))

print('''Choose character set for password from these :
1. Digits
2. Letters
3. Special characters
4. Exit''')

characterList = ""

# Getting character set for password
while(True):
choice = int(input("Pick a number "))
if(choice == 1):

# Adding letters to possible characters
characterList += string.ascii_letters
elif(choice == 2):

# Adding digits to possible characters
characterList += string.digits
elif(choice == 3):

# Adding special characters to possible
# characters
characterList += string.punctuation
elif(choice == 4):
break
else:
print("Please pick a valid option!")

password = []

for i in range(length):

# Picking a random character from our
# character list
randomchar = random.choice(characterList)

# appending a random character to password
password.append(randomchar)

# printing password as a string
print("The random password is " + "".join(password))

Output:

Input 1: Taking only letters in the character set

Create a Random Password Generator using Python

How to Create a Random Password Generator using Python - Totneo

Input 2: Taking letters and numbers both

 

Create a Random Password Generator using Python

How to Create a Random Password Generator using Python - Totneo

Input 3: Using special characters, numbers, and letters

 

How to Create a Random Password Generator using Python - Totneo

Create a Random Password Generator using Python

Strong Password Generator Only by entering the size of password

 

Implementation2

python3

# import modules
import string
import random

# store all characters in lists
s1 = list(string.ascii_lowercase)
s2 = list(string.ascii_uppercase)
s3 = list(string.digits)
s4 = list(string.punctuation)

# Ask user about the number of characters
user_input = input("How many characters do you want in your password? ")

# check this input is it number? is it more than 8?
while True:

try:

characters_number = int(user_input)

if characters_number < 8:

print("Your number should be at least 8.")

user_input = input("Please, Enter your number again: ")

else:

break

except:

print("Please, Enter numbers only.")

user_input = input("How many characters do you want in your password? ")

# shuffle all lists
random.shuffle(s1)
random.shuffle(s2)
random.shuffle(s3)
random.shuffle(s4)

# calculate 30% & 20% of number of characters
part1 = round(characters_number * (30/100))
part2 = round(characters_number * (20/100))

# generation of the password (60% letters and 40% digits & punctuations)
result = []

for x in range(part1):

result.append(s1[x])
result.append(s2[x])

for x in range(part2):

result.append(s3[x])
result.append(s4[x])

# shuffle result
random.shuffle(result)

# join result
password = "".join(result)
print("Strong Password: ", password)

 

Output:

How to Create a Random Password Generator using Python - Totneo

Share This Article
Leave a comment

Leave a Reply

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