Untitled

mail@pastecode.io avatar
unknown
python
8 months ago
1.3 kB
1
Indexable
Never
import random
import string

def generate_password(length, uppercase=True, lowercase=True, digits=True, symbols=True):
    """Generates a random password with specified criteria.

    Args:
        length (int): The desired length of the password.
        uppercase (bool, optional): Whether to include uppercase letters. Defaults to True.
        lowercase (bool, optional): Whether to include lowercase letters. Defaults to True.
        digits (bool, optional): Whether to include digits. Defaults to True.
        symbols (bool, optional): Whether to include symbols. Defaults to True.

    Returns:
        str: The generated password.
    """

    character_pools = []
    if uppercase:
        character_pools.append(string.ascii_uppercase)
    if lowercase:
        character_pools.append(string.ascii_lowercase)
    if digits:
        character_pools.append(string.digits)
    if symbols:
        character_pools.append(string.punctuation)

    all_characters = ''.join(character_pools)
    password = ''.join(random.choice(all_characters) for _ in range(length))
    return password

# Example usage:
password = generate_password(12, uppercase=True, lowercase=True, digits=True, symbols=False)
print(password)  # Example output: 9hG543jKbXd
Leave a Comment