Generating Random Passwords with Customizable Length

By: fyvo July 29, 2025 Python

Description

This Python script generates strong, random passwords. Users can specify the desired length, including options for uppercase, lowercase, numbers, and symbols.

Code Snippet

import random
import string

def generate_password(length, uppercase=True, lowercase=True, numbers=True, symbols=True):
    characters = ''
    if uppercase:
        characters += string.ascii_uppercase
    if lowercase:
        characters += string.ascii_lowercase
    if numbers:
        characters += string.digits
    if symbols:
        characters += string.punctuation

    if not characters:
        return "Please select at least one character type"

    password = ''.join(random.choice(characters) for i in range(length))
    return password

# Example usage:
password = generate_password(12, uppercase=True, lowercase=True, numbers=True, symbols=True)
print(f"Generated Password: {password}")
password = generate_password(8, uppercase=False, lowercase=True, numbers=True, symbols=False)
print(f"Generated Password: {password}")

Discussion (0)