Lazo Lab Sign up

Python Projects & Skills / Security tools in Python

Lesson 25 of 28

Password generator

Use the secrets module for anything security related. random is fine for games but predictable for passwords.

Key points

  • secrets.choice for each character
  • Length 16+
  • Never use random for passwords
import secrets, string
chars = string.ascii_letters + string.digits + "!@#$%"
print("".join(secrets.choice(chars) for _ in range(16)))
Watch a video on thisOpens YouTube search results for “Password generator” in a new tab

Quiz · +10 XP

Which module should generate passwords?

Log in to save progress and earn XP.