Create a Strong Password Generator in Python

password generator in python

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

Share your love
Subhankar Rakshit
Subhankar Rakshit

Hey there! Iā€™m Subhankar Rakshit, the brains behind PySeek. Iā€™m a Post Graduate in Computer Science. PySeek is where I channel my love for Python programming and share it with the world through engaging and informative blogs.

Articles:Ā 198