secrets.token_urlsafe()
secrets.token_urlsafe() is a Python function in the Secrets Module category. Returns a URL-safe random string (base64 encoded, no padding). The syntax is secrets.token_urlsafe(nbytes=None). Returns: URL-safe string. A typical example: secrets.token_urlsafe(32) # "xYZabc...-token-string". A close sibling is secrets.token_bytes(), which returns cryptographically secure random bytes (for tokens, keys, etc.). A close sibling is secrets.token_hex(), which returns a random hex string (for secure tokens). A close sibling is secrets.compare_digest(), which compares two strings in constant time (mitigates timing attacks). A close sibling is secrets.choice(), which returns a cryptographically secure random element from a sequence. More about this category: Cryptographic secure random — token_bytes, token_hex, compare_digest. Related Secrets Module entries: secrets.token_bytes(), secrets.token_hex(), secrets.compare_digest(), secrets.choice(), secrets.randbelow(), secrets.SystemRandom(). This page is part of the free Python Reference documentation covering 978+ functions, methods, and modules with examples and parameter details. You can try secrets.token_urlsafe() immediately in the Python playground, look up unfamiliar terms in the glossary, or return to the Python Reference homepage.