Skip to content

Generators

generate_random_id

def generate_random_id(chars=7): ...

Generates a random alphanumeric string (ASCII letters and digits) of the given length. Defaults to 7 characters.

Parameter Type Default Description
chars int 7 Length of the generated id
generate_random_id()        # e.g. "aZ3kLo9"
generate_random_id(chars=12)

Note

Characters are drawn with secrets.choice, so the output cannot be predicted from earlier ids. The default length of 7 is still short (about 41 bits): fine for readable ids, too small for a token or invite code. For those, pass a longer chars (22 or more) or use secrets.token_urlsafe().