import random
import string
def generate_password(length=12):
# Define character sets
letters = string.ascii_letters # a-z, A-Z
digits = string.digits # 0-9
special_chars = string.punctuation # Special characters like !, @, #, etc.
# Combine all character sets
all_chars = letters + digits + special_chars
# Ensure the password contains at least one character from each category
password = [
random.choice(letters),
random.choice(digits),
random.choice(special_chars)
]
# Fill the rest of the password length with random choices from all characters
password += random.choices(all_chars, k=length - 3)
# Shuffle the resulting password list to avoid predictable patterns
random.shuffle(password)
# Convert the list into a string and return it
return ''.join(password)
# Example usage
length = int(input("Enter the desired password length: "))
print("Generated password:", generate_password(length))