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.
Quiz · +10 XP
Which module should generate passwords?
Log in to save progress and earn XP.