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.
To build a random password generator, we’ll use this approach:
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:
random
the module used to generate random numberspassword_list
random.choice()
method for each of the character’s declared variablespassword_list
changing the position of the elements every time a new password using the .shuffle()
method
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:
password
for
keywordchar
variable.join()
method to change the list iterate from the password list into a stringf-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}")
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.