Software Engineering

How to Create a Password Generator in Python


You can easily create a password generator in Python with the following simple method.

How to create the Password Generator in Python

import string
import random

def generate_password(length=16):
    seed = f"{string.ascii_letters}{string.ascii_lowercase}{string.ascii_uppercase}{string.punctuation}"

    password_string = [x for x in seed]
    random.shuffle(password_string)
    return ''.join(password_string[:length])

How to use the Password Generator in Python

With this function, you can either call the generate_password() function without any arguments, and it will generate a 16 digit long password, unless you need a longer one, then pass in an integer to specify the lengh of the password you would like. e.g. generate_password(28).

How to Test the Password Generator in Python

Now we can test it as follows:

print(generate_password())
# rE(_&pmYxoj^NEvL

print(generate_password(28))
# [email protected]+"BjGbR