Password Generator in Python

Password Generator in Python

3 mins read419 Views Comment
Updated on Jul 11, 2024 11:16 IST

This is a quick guide for those who want to generate strong password using python. This article includes step by step explanation of programming and included all steps needed to generate a new password for you.

2023_05_MicrosoftTeams-image-14.jpg

Creating and managing passwords can be daunting, especially when you need to create strong, unique passwords for different accounts. A password generator is a tool that can help easily generate random, strong passwords. In this article, we’ll learn how to create a password generator in Python using string and random modules.

Table of contents

Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

– / –
40 hours
– / –
5 days
– / –
3 days
3 K
3 weeks
– / –
4 days
– / –
20 hours
– / –
2 months
Free
6 weeks

Code generator python

Step 1: Importing the Required Modules

First, import the required modules. We will use random module to generate random characters and the string module to get a list of valid characters we can use in our password. Here’s the code:


 
import random
import string
Copy code

If you want to use functions to produce random numbers, then import a random module,

If you want to use the collection of constants representing sets of characters, such as lowercase and uppercase letters, digits, and punctuation, then you have to import the string module.

Renaming Columns in Pandas
Find the Second Occurrence of a Substring in Python String
How to use pass statement in Python

Must check:Python Online Course and Certification

Step 2: Concatenating the constants

We need set of characters from which we want to create the password. We can do this by concatenating the constants from the string module:


 
all_characters = string.ascii_letters + string.digits + string.punctuation
Copy code

This will create a string that contains all the ASCII letters, digits, and punctuation marks.

Now, we’ll ask the user for the length of the password they want to generate.

Must Read: Why use Python Datetime module?

Must Read: Free QR generator using Python

Step-3: Asking the user to tell the length of a password 


 
length = int(input("Enter the length of the password: "))
Copy code

The input function -It takes a prompt as an argument and returns the user’s input as a string. We convert the input to an integer using the int function.

Step-4 Generating the password

After taking the length of the password, we can generate a password of the desired length using the function of the choice from the random module:


 
password = ''.join(random.choices(all_characters, k=length))
Copy code

The choices function takes two arguments: a sequence of items to choose from and the number of items. We pass the all_characters string and the password length as arguments in this case. The function returns a list of randomly selected characters, which we combine into a single string using the join method.

How to Generate Random Numbers in Python?
Difference between ASCII and Unicode
Python Input Function: A Comprehensive Guide to Getting User Input in Python

Also check out: Difference Between Module and Package in Python

Step 5: Printing the password


 
print("Your password is:", password)
Copy code

This will print the newly generated password.

Putting everything together


 
# import the string and random modules
import string
import random
# define the characters that can be used in the password
all_characters = string.ascii_letters + string.digits + string.punctuation
# ask the user for the desired length of the password
length = int(input("Enter the length of the password: "))
# generate a password using randomly chosen characters
# using the 'choices' function from the random module
# and joining the resulting characters into a string
password = ''.join(random.choices(all_characters, k=length))
# display the generated password to the user
print("Your password is:", password)
Copy code

Output:

Enter the length of the password: 7
Your password is: VARJ)&q

For generating multiple passwords


 
import random
import string
def generate_password(length, count):
passwords = []
for i in range(count):
password = ''.join(random.choice(string.ascii_letters + string.digits + string.punctuation) for _ in range(length))
passwords.append(password)
return passwords
Copy code

This function takes in two arguments: length is the length of each password, and count is the number of passwords to generate. Python’s

random
Copy code
module is used to generate random characters from the set of uppercase and lowercase letters, digits, and punctuation symbols defined in the
string
Copy code
module. It then concatenates these characters into a string to form a password.


 
passwords = generate_password(8, 5)
print(passwords)
Copy code

Output:

[‘Lwe%Oz+y’, ‘~=jd_S(}’, ‘w#>z;mZl’, ‘@#KX+4E7’, ‘(4ywvb”q’]

This will output a list of 5 passwords, each consisting of 8 characters.

Conclusion

You can easily generate strong passwords of any length using this above code. But don’t only rely on this password generation method for the security of your data. It is advisable to follow best practices for password security, such as using a different password for each account and regularly updating your passwords. If this article helped you, please like and share it with your friends.

About the Author