On this page
secrets.choice
Up: secrets | Next: secrets.compare_digest
Rel: secrets.compare_digest, secrets.randbelow, secrets.randbits, secrets.SystemRandom
Ref: docs.python.org — secrets.choice
Tags: #public #python3 #stdlib #secrets
Import
import secrets
from secrets import choice
Signature & Overview
secrets.choice(seq)
Choose a random element from a non-empty sequence.
Usage
import secrets
import string
alphabet = string.ascii_letters + string.digits
code = "".join(secrets.choice(alphabet) for _ in range(12))
assert len(code) == 12
Gotchas
- Raises
IndexErroron empty sequences: Same interface asrandom.choice(), but backed by the OS CSPRNG.
REPL Inspect
from secrets import choice
help(choice)
Linked from
- tuts/python3/stdlib_module/secrets
- tuts/python3/stdlib_module/secrets/compare_digest
- tuts/python3/stdlib_module/secrets/randbelow
- tuts/python3/stdlib_module/secrets/randbits
- tuts/python3/stdlib_module/secrets/SystemRandom
- tuts/python3/stdlib_module/secrets/token_bytes
- tuts/python3/stdlib_module/secrets/token_hex
- tuts/python3/stdlib_module/secrets/token_urlsafe
Related notes
- tuts/python3/stdlib_module/secrets/compare_digest Linked here; links to this page; shared tags: #secrets, #stdlib
- tuts/python3/stdlib_module/secrets/randbelow Linked here; links to this page; shared tags: #secrets, #stdlib
- tuts/python3/stdlib_module/secrets/randbits Linked here; links to this page; shared tags: #secrets, #stdlib
- tuts/python3/stdlib_module/secrets/SystemRandom Linked here; links to this page; shared tags: #secrets, #stdlib
- tuts/python3/stdlib_module/secrets Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/secrets/token_bytes links to this page; shared tags: #secrets, #stdlib
- tuts/python3/stdlib_module/secrets/token_hex links to this page; shared tags: #secrets, #stdlib
- tuts/python3/stdlib_module/secrets/token_urlsafe links to this page; shared tags: #secrets, #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib