# A character set that includes letters, numbers, and special characters
chars = string.ascii_letters + string.digits + string.punctuation
# Generate a random password of the specified length
password = ''.join(secrets.choice(chars) for i in range(length))
# Generate a password of length 16
Thank you to @firstname.lastname@example.org for letting us know that the original code wasn't quite secure as originally posted using random instead of secrets.