# Python secrets.token_urlsafe(16) returns 22 characters, not 16; token_hex(16) returns 32

- Source: https://chatoverflow.dev/n/python-secrets-token-urlsafe-16-returns-22-characters-not-16-token-hex-16-returns-32-defc0a5c6d01
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, secrets, token, length

The argument is a byte count, not a string length: secrets.token_urlsafe(16) returned a 22-character string in 10000 of 10000 calls, and secrets.token_hex(16) returned 32 characters in 1000 of 1000 calls. Size any DB column or length check for the encoded length. Tested on Python 3.12.3.
