App 3: Password Generator & Checker / Build the password tool
Lesson 3 of 10
Generate a password
Build a pool of characters with the string module, then pick from it secrets.choice times in a row.
Key points
- string.ascii_letters, string.digits, string.punctuation
- "".join() glues characters together
- Default length: 16
import secrets, string
def make_password(length=16):
pool = string.ascii_letters + string.digits + "!@#$%^&*-_"
return "".join(secrets.choice(pool) for _ in range(length))
print(make_password())Your turn
Add make_password() and let the user choose the length.
Quiz · +10 XP
What does "".join(["a", "b", "c"]) return?
Log in to save progress and earn XP.