One of the most common things on the internet today is passwords. They provide safe access to a lot of online platforms. They provide a defense against unauthorised access to your personal information or computer. Strong passwords make it harder for hackers to get access to your information or computer. It's advisable to use strong passwords. This Python program will help you create a strong password.
In this article, I will show you how I built a password generator using Python. The password generator generates a password with a mix of uppercase letters, lowercase letters, symbols, and numbers.
The basic concepts used were random module, input(), range(), list and a for loop.
Random module
One thing about Python is modules. Modules are used a lot in python. It is a file containing statements and definitions. You use them in your program by importing them. They provide us a way to reuse code.
I made use of the random module. It is used to perform random generations.
The first thing I did was to import random
to be able to access the Python library.
import random
Assigning characters to the character variable
I assigned characters to the character variable. We have 26 letters in the English alphabet. Here, I created the variable characters
and added the letters, symbols and numbers together.
characters = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@£$%^&*().,?#0123456789")
There is another way this can be done, as seen below, you can store the elements in a list. It makes it more readable. They can be separated this way:
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”]
numbers = [“0”, “1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”]
symbols=[“!”, “#”, “$”, “%”, “^”, “&”, “(“, “)”, “*”, “+”, “-” ]
Getting the number of passwords the user needs
After the characters were assigned the next thing I did was to use the input()
function to prompt the user to enter the number of passwords they want to generate.
number = input(“How many passwords do you want to generate?: ”)
number = int(number)
Note: int(number)
helps with type conversion. int
means integer, In python and some programming languages like C, it means number.
I used number
as the variable name because I didn’t want the name to be long, I would have used num_of_passwords
to make the code more readable.
Choosing the password length
The user gets to select the length of the password they want.
length = input(“Input the length of your password?: “)
length= int(length)
Using the print ()
function after you select the length of your password, the program ends
print(“\nhere are your passwords: “)
Writing the for loop
The for loop would loop through the list I created. It would do this anytime the user makes an entry. I also used the random.choice
method to return randomly selected elements from the list. It's put this way choice()
Finally, the range()
function. It returns a sequence of numbers from 0 and increments by 1 and stops before a specified number.
for password in range(number):
passwords =””
for c in range(length):
passwords += random.chioce(characters)
And finally print the password.
`print(passwords)`
The whole code will look like this;
#!/usr/bin/python3
"""
The random is a python module that generates random numbers
"""
import random
print("Password Generator")
# Character contains all the characters needed to generate the password.
characters = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@£$%^&*().,?#0123456789")
number = input("How many passwords do you want to generate?: ")
number = int(number)
# Length helps you select the length of your password
length = input("Input the length of your password: ")
length = int(length)
print("\nhere are your passwords: ")
# A simple for loop function that helps the user get the password
for password in range(number):
passwords = ""
for c in range(length):
passwords += random.choice(characters)
print(passwords)
The Output will look like this:
PS C:\Users\USER\Desktop\Practice-file\password-generator> python password.py
Password Generator
How many passwords do you want to generate?: 9
Input the length of your password: 10
here are your passwords:
ck3wBf93%s
Un0IHb5Lej
0RdeJ8vIUL
19Yd,0ov%Y
f,ZZN(#(NJ
2aaM$z23hf
!#TBc#Be7L
FiyBG)^QO£
ji0IoVK(*Y
PS C:\Users\USER\Desktop\Practice-file\password-generator>
It works! I hope with the steps, you’ve got an understanding of how to build a password generator in python.
You can find the code in my Github repository
Thank you for taking time to read this article. I will appreciate your feedback.