How to Create a Random Password Generator Using Python

0

Every website has some form of security interface that requires user authentication. These forms often use your email address and password to access the website. Using a secure password when logging in is essential to prevent bad guys (hackers) from accessing your account.

This article will teach you how to create a random password generator using Python by generating a combination of letters, numbers, and symbols as scrambled characters, making it difficult to crack or guess the password. .

Let’s build a secure random password generator together.

Begin

To create a random password generator, we’ll use this approach:

  • Write all acceptable password character types, such as letters, numbers, and symbols
  • Give users the option to enter the number of letters, symbols, and numbers for the generated password
  • Randomize character order to make it hard to guess

Creation of the random password generator

As you know, some apps on the internet suggest random passwords when you create a new account. Random characters are up to you and can be as long as eight characters.

Create a new file, main.pyto write application scripts.

# main.py


letters = [
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
    'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
    'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
    'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
]
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']

print("Welcome to the PyPassword Generator!")

nr_letters = int(input("How many letters would you like in your password?n"))

nr_symbols = int(input(f"How many symbols would you like?n"))

nr_numbers = int(input(f"How many numbers would you like?n"))

The characters in the code block above are the combination of the password generator presented in a list.

Next, make sure users can enter a number, an integer representing the number of times a character will appear when the final output is displayed and declared with a variable.

n: represents that the input value will go to the next line

Now let’s update the rest of the code. Copy and paste the following:

# main.py
# Password Generator Project

import random # add this

# letters, numbers, and symbols lists

# users' input for the amount of characters

# add these below
password_list = []

for char in range(1, nr_letters + 1):
    password_list.append(random.choice(letters))

for char in range(1, nr_symbols + 1):
    password_list.append(random.choice(numbers))

for char in range(1, nr_numbers + 1):
    password_list.append(random.choice(symbols))

random.shuffle(password_list)

The code block does the following:

  • Import the built-in random module used to generate random numbers
  • Create an empty list [] with variable, password_list
  • Iterate through the number in the range function to create a sequence of numbers starting at the start index and ending with the last index plus 1
  • Then add the empty list to get a randomly selected item using the random.choice() method for each of the characters declared as variable
  • Mix the new password_list change the position of the elements each time a new password using the .shuffle() method

Convert password list to string

Copy and update the following code:

# main.py

# import

# letters, numbers, and symbols lists

# users' input for the amount of characters

# randomize characters

# add this
password = ""
for char in password_list:
    password += char

# convert list to string
pwd = ''.join(password_list)
print(f"Your random password to use is: {pwd}")

The process of converting a list to a string is as follows:

  • Create an empty string variable, password
  • Scroll through the list of passwords using the for keyword
  • Concatenate password strings with loop char variable
  • Use the .join() method to change the iterate list of password list in a string
  • Finally, display the result of the password using the f-strings

The end result of the code:

# main.py

import random

letters = [
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
    'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
    'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
    'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
]
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']

print("Welcome to the PyPassword Generator!")
nr_letters = int(input("How many letters would you like in your password?n"))
nr_symbols = int(input(f"How many symbols would you like?n"))
nr_numbers = int(input(f"How many numbers would you like?n"))

password_list = []

for char in range(1, nr_letters + 1):
    password_list.append(random.choice(letters))

for char in range(1, nr_symbols + 1):
    password_list.append(random.choice(numbers))

for char in range(1, nr_numbers + 1):
    password_list.append(random.choice(symbols))

random.shuffle(password_list)

password = ""
for char in password_list:
    password += char
print("char", char)

# convert list to string
pwd = ''.join(password_list)
print(f"Your random password to use is: {pwd}")

Conclusion

In this article, you have developed an application that generates random passwords that are not the same on each attempt, which makes it dynamic to generate as many passwords as possible.

Learn more

LOADING
. . . comments & After!

Share.

About Author

Comments are closed.