DevToolsForYou

UUID Generator in Python — Code Examples

UUID Generator in PythonUse the online tool →

UUIDs (Universally Unique Identifiers) are 128-bit identifiers used as database primary keys, session tokens, and idempotency keys. UUID v4 (random) is the safe default; UUID v7 (time-ordered) is preferred for database primary keys.

Python's standard library includes the uuid module with support for v1, v3, v4, and v5.

Python 3
import uuid

# UUID v4 — random (most common)
id_v4 = uuid.uuid4()
print(id_v4)           # e.g. 110e8400-e29b-41d4-a716-446655440000
print(str(id_v4))      # string representation
print(id_v4.hex)       # without hyphens
print(int(id_v4))      # as integer

# UUID v1 — time-based
id_v1 = uuid.uuid1()
print(id_v1)

# UUID v5 — deterministic (namespace + name)
id_v5 = uuid.uuid5(uuid.NAMESPACE_URL, "https://example.com")
print(id_v5)  # always the same for the same input

# Generate multiple
ids = [str(uuid.uuid4()) for _ in range(5)]
print(ids)
Notes & gotchas
  • uuid.uuid4() uses os.urandom() internally — it is cryptographically secure.
  • uuid.uuid5() generates a deterministic UUID from a namespace and name — useful for idempotent ID generation.
  • Python does not have built-in UUID v7 support yet — use the uuid6 or uuid7 package.
Try it in your browser

Need to uuid generator without writing code? The UUID Generator runs entirely in your browser — paste your input and get the result instantly. No signup, no install, no data sent to a server.

Open UUID Generator
UUID Generator in other languages