Generate a Random Secure Password with Python

You may use the following Python script to generate a random secure password. The random secure password may include lowercase letters, uppercase letters, numbers and special characters.

Generate a Random Secure Password with Python
Photo by Franck / Unsplash

You may use the following Python script to generate a random secure password. The random secure password may include lowercase letters, uppercase letters, numbers and special characters.

import random
import string

def generate_password(length):
    # Define a string of all possible characters for the password
    characters = string.ascii_letters + string.digits + string.punctuation

    # Generate a random password with the specified length
    password = ''.join(random.choice(characters) for i in range(length))

    return password

# Generate a password with length 15
password = generate_password(15)
print(password)

By default the length of the password is 15 characters. You may change the length of the password on line 14.