Lazo Lab Sign up

App 3: Password Generator & Checker / Build the password tool

Lesson 2 of 10

The secrets module

Python's random module is predictable, which is fine for games but not passwords. The secrets module uses the operating system's secure randomness.

Key points

  • secrets.choice picks securely
  • secrets.token_urlsafe makes tokens
  • Never use random for passwords
import secrets
print(secrets.choice(["a", "b", "c"]))
print(secrets.token_urlsafe(16))

Your turn

Run the example three times and see that the output changes every time.

Watch a video on thisOpens YouTube search results for “The secrets module” in a new tab

Quiz · +10 XP

Which module should generate passwords?

Log in to save progress and earn XP.