all Python 3uuidfunction
uuid.uuid4()
generating UUIDs
A random version 4 UUID from a cryptographically secure generator — the usual pick when you need a unique identifier.
Syntax
uuid.uuid4()
It has 122 random bits, not 128: six are taken by the version and variant, so the third group always starts with 4. A collision is impossible in practice, but there is no order: as a database primary key a random UUID scatters inserts across the index, which is why uuid7() arrived in 3.14. For a shorter string in a link use u.hex (32 characters) or secrets.token_urlsafe().
Examples
import uuid
u = uuid.uuid4()
print(u) # → ? 1b4e28ba-2fa1-41d2-883f-0016d3cca427 — случайное
print(u.version, len(str(u)), len(u.hex)) # → 4 36 32
print(str(u)[14]) # → 4
See also
Open in the full glossarydocs.python.orgReport an inaccuracy