UUID generator in Python
Python has UUID generation built in. Here are copy-ready examples for v4, v7 and v1, plus formatting. Prefer to generate in the browser? Use our free online UUID generator.
Generate a random UUID (v4)
The standard library's uuid module handles this. uuid4() draws 122 random bits from the OS and returns a UUID object.
import uuid
u = uuid.uuid4()
print(u) # 550e8400-e29b-41d4-a716-446655440000
print(type(u)) # <class 'uuid.UUID'>
Convert to text with str(u), or read the attributes directly. The v4 page explains why this is the usual default.
No hyphens, uppercase, braces
u = uuid.uuid4()
print(u.hex) # 32 hex digits, no hyphens
print(str(u).upper()) # UPPERCASE
print('{' + str(u) + '}') # {with-braces}
Generate a time-sortable UUID (v7)
Newer Python releases include uuid.uuid7(), which embeds the current millisecond timestamp so values sort in creation order. If your runtime does not have it yet, you can build the same layout yourself:
import time, uuid, os
def uuid7():
ms = int(time.time() * 1000) & ((1 << 48) - 1)
rand = int.from_bytes(os.urandom(10), 'big') & ((1 << 74) - 1)
val = (ms << 80) | (0x7 << 76) | ((rand >> 62) << 64) | (0x2 << 62) | (rand & ((1 << 62) - 1))
return uuid.UUID(int=val)
print(uuid7())
Use v7 for database keys where ordered inserts improve index locality. The v7 generator produces the same format without code.
Generate a time-based UUID (v1)
u = uuid.uuid1()
print(u) # includes timestamp + node (MAC)
Note that v1 exposes a timestamp and, historically, the machine's MAC address. For opaque identifiers prefer v4. See the comparison on the home page.
Validate and parse a UUID string
s = '550e8400-e29b-41d4-a716-446655440000'
try:
u = uuid.UUID(s)
print('valid, version', u.version) # valid, version 4
except ValueError:
print('not a UUID')
The version attribute tells you which algorithm produced the ID. For a quick check without code, paste it into the online validator.
Working with UUID objects
A UUID object is more useful than a raw string in many cases. It compares and sorts correctly, and it exposes useful attributes such as version, variant, hex and bytes. You can build one from a string with uuid.UUID(s), from an integer with uuid.UUID(int=n), or from bytes with uuid.UUID(bytes=b). Passing a malformed string raises ValueError, which makes the constructor a convenient validator as shown above.
Note that UUID objects are immutable and hashable, so they can be used directly as dictionary keys or stored in a set. When you need to persist them, convert to str() for a human-readable column or .bytes for a compact 16-byte binary column — the latter halves storage in databases that support it.
Generate many UUIDs
ids = [str(uuid.uuid4()) for _ in range(1000)]
That is all you need for seed data or test fixtures. If you would rather not write code, the bulk generator gives you up to 1000 IDs, one per line, ready to copy.
Frequently asked questions
How do I generate a UUID in Python?
Import uuid and call uuid.uuid4(); use str() for the text form.
How do I generate a UUID v7 in Python?
Use uuid.uuid7() on recent versions, or build the timestamp-plus-random layout shown above on older runtimes.
uuid4() vs uuid1()?
uuid4() is random and opaque; uuid1() embeds timestamp and node. Prefer uuid4().
How to get a UUID without hyphens?
Use uuid.uuid4().hex for 32 lowercase hex digits with no separators.
Online UUID generator →
Generate IDs in the browser, no Python needed.
UUID v7 explained →
Why time-sortable IDs help database indexes.