import secrets import string # 1. Define your character groups letters = string.ascii_letters digits = string.digits special = "!@#$%^&" # 2. Combine all characters for the "pool" all_characters = letters + digits + special # 3. Start the password with one guaranteed character from each group password_list = [ secrets.choice(letters), secrets.choice(digits), secrets.choice(special) ] # 4. Fill the remaining length (16 - 3 = 13 characters) from the full pool password_list += [secrets.choice(all_characters) for _ in range(13)] # 5. Shuffle the list so the guaranteed characters aren't always at the start # We use secrets.SystemRandom() to ensure the shuffle is cryptographically secure secrets.SystemRandom().shuffle(password_list) # 6. Join into a single string password = "".join(password_list) print(password)