Прочитайте документы: https://docs.python.org/3/library/secrets.html
Я скопировал этот пример из документов Python и добавил специальные символы в пароль.
import secrets import string alphabet = string.ascii_letters + string.digits + '!@#$%^&*()-+[]' while True: password = ''.join(secrets.choice(alphabet) for i in range(10)) if (any(c.islower() for c in password) and any(c.isupper() for c in password) and any(c.isalnum() for c in password) and any(not(c.isalnum()) for c in password) and any(c.isdigit() for c in password)): break
Оригинал: “https://dev.to/angelacpd/generate-random-passwords-in-python-using-secrets-33hb”