Gavin Wiggins


Secrets in Python

Published on November 8, 2022
Tagged with python

Use the secrets module to generate cryptographically strong random passwords containing letters, numbers, and punctuation.

import string
import secrets

n = 20
chars = string.ascii_letters + string.digits + string.punctuation
password = ''.join(secrets.choice(chars) for _ in range(n))

print('password', password)

The code example shown above produces a 20-character password such as:

password ^sNw3?-!rF9U{O$J=2cW

or something like

password PU^1g\q6I(`lf4qz5]N[

Gavin Wiggins © 2026
Made on a Mac with Genja. Hosted on GitHub Pages.