
Introduction
Imagine, youāre unlocking your front door with a paperclip. Sounds crazy, right? Well, using a weak password to guard your online life is just as risky. Hackers are like sneaky paperclip-wielding neighbors, always looking for an easy way in.
Thatās why strong passwords are super important! Theyāre like sturdy locks that keep the bad guys out. But coming up with tough-to-crack passwords for every single website can be a real headache.
Luckily, we have a solution. Letās build a password-creating machine using Python! This helpful tool will whip up strong, random passwords for you in seconds. So, letās dive in and learn how to create a Password Generator in Python!
Visit Also: Create a Simple Brute-Force Password Cracker in Python
Import the Random Module
First, import the random module that we can use to generate random characters and strings.
import random
Define the Password Length
Now define the length of the password. A secure password should be at least eight characters long, but the longer the password, the more secure it is.
password_length = int(input("Enter the password length: "))
Generate the Password
We will use a combination of lowercase and uppercase letters, numbers, and symbols. You can define these characters in a string and then use the random
Ā module to select random characters from the string.
characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()?" password = "".join(random.sample(characters, password_length))
In the code above, we used the ājoinā method to join the randomly selected characters into a string. We also used the āsampleā method to select the characters randomly from the ācharactersā string.
Print the Generated Password
Letās print the generated password.
print("Your generated password is: ", password)
Here is the complete code for the password generator:
import random password_length = int(input("Enter the password length: ")) characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()?" password = "".join(random.sample(characters, password_length)) print("Your generated password is: ", password
You can save the above code in a file with a ā.pyā extension and run it from the terminal or command prompt using the following command:
python password_generator.py
Output
The above program outputs the following:
Enter the password length: 12
Your generated password is: y19lz#3$^TFX