paint-brush
How to Create a Random Password Generator Using Pythonby@terieyenike
30,528 reads
30,528 reads

How to Create a Random Password Generator Using Python

by TeriSeptember 3rd, 2022
Read on Terminal Reader
Read this story w/o Javascript

Too Long; Didn't Read

Using a secure password when logging in is essential to prevent the bad guys (hackers) from gaining access to your account. This article will teach you how to create a random password generator using Python. We’ll use this approach: Write out all acceptable passwords character types, such as the letters, numbers, and symbols. Give users the ability to enter the number of letters, symbols, and numbers for the generated password. Randomize the order of characters to make it hard to guess the password.
featured image - How to Create a Random Password Generator Using Python
Teri HackerNoon profile picture


Every website has some form of security interface requiring user authentication. These forms often use your email and password to access the website. Using a secure password when logging in is essential to prevent the bad guys (hackers) from gaining access to 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 characters scrambled together, making it difficult to crack or guess the password.


Let’s build a random password generator together.


Getting Started

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

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


Creating the Random Password Generator

As you know, some applications on the internet suggest randomized passwords when you create a new account. The randomized characters are for you to decide and can be as long as eight characters.


Create a new file, main.py, to write the scripts for the app.


# 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 from the code block above make up the combination of the password generator presented in a list.


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


\n: represent that the input value will go to the following 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 in-built random the module used to generate random numbers
  • Create an empty list [] with the variable, password_list
  • Iterate through the number in the range function to create a sequence of numbers from the start index and end with the last index plus 1
  • Next, append the empty list to get a randomly selected element using the random.choice() method for each of the character’s declared variables
  • Shuffle the newly created password_list changing the position of the elements every time a new password using the .shuffle() method


Convert the Password List to a 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
  • Iterate through the password list using the for keyword
  • Concatenate the password strings with the looped char variable
  • Use the .join() method to change the list iterate from the password list into a string
  • Finally, display the result of the password using the f-strings


The final 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 developed an application that generates random passwords that are not the same on each try, making it use case dynamic for generating as many passwords as possible.


Though this may not be the best approach to generating random passwords, the article's essence is to show the possibility of using the Python program to create a password. Still, the output is unexpected and different with every new input, no matter the length of the password.




Learn More