Python’s built-in uuid module generates UUIDs (Universally Unique Identifiers)—also called GUIDs on some platforms. For most random unique IDs, use uuid.uuid4(). Use uuid.uuid5() when the same namespace and name must always yield the same UUID.
Use uuid4() for request IDs, filenames, and database keys where you do not need time ordering. Avoid uuid1() when privacy matters—it can embed time and host-related information. UUIDs identify resources; they are not passwords or session secrets by default.
Tested on: Python 3.13.3; kernel 6.14.0-37-generic.
Python UUID quick reference
| Task | Use |
|---|---|
| Generate a random UUID | uuid.uuid4() |
| Generate a time and host-based UUID | uuid.uuid1() |
| Generate a deterministic UUID using MD5 | uuid.uuid3(namespace, name) |
| Generate a deterministic UUID using SHA-1 | uuid.uuid5(namespace, name) |
| Convert UUID to string | str(uuid_obj) |
| Get UUID without hyphens | uuid_obj.hex |
| Create UUID from string | uuid.UUID(uuid_string) |
| Generate UUID from command line | python -m uuid |
| Generate a short UUID | shortuuid library or hex/truncation (collision risk) |
| Use GUID in Python | import uuid |
What is a UUID in Python?
A UUID is a 128-bit identifier, usually shown as 8-4-4-4-12 hex digits with hyphens—for example 550e8400-e29b-41d4-a716-446655440000. Python’s uuid module creates UUID objects and functions for versions 1, 3, 4, and 5 (plus 6, 7, and 8 in Python 3.14+).
GUID (globally unique identifier) and UUID refer to the same kind of value in everyday Python code—there is no separate guid module.
Generate random UUID in Python using uuid4()
The fastest answer to “generate UUID in Python”:
import uuid
new_id = uuid.uuid4()
print(new_id)
print(str(new_id))Each call produces a new random UUID. The official docs describe uuid4() as using a cryptographically secure pseudo-random generator.
Use uuid4() for random IDs, tokens, filenames, and primary keys when you do not need IDs sorted by creation time.
Convert UUID to string, hex, bytes, and int
import uuid
u = uuid.uuid4()
print(str(u))
print(u.hex)
print(len(u.hex))
print(len(u.bytes))
print(u.int.bit_length())You should see the hyphenated string, 32 hex characters without hyphens, 16 raw bytes, and 128 bits as an integer. Store str(u) in JSON, databases, and logs; use u.hex when hyphens are not allowed.
import json
# UUID objects are not JSON-serializable by default
json.dumps({"id": str(uuid.uuid4())})Generate multiple UUIDs in Python
import uuid
ids = [uuid.uuid4() for _ in range(5)]
for i, value in enumerate(ids):
print(i, value)Each uuid4() call is independent—fine for test data, batch filenames, or seeding records.
Create UUID from string
Parse a UUID read from a config file, API, or database:
import uuid
text = "550e8400-e29b-41d4-a716-446655440000"
u = uuid.UUID(text)
print(u)
print(u.version)Hyphens are optional in some forms; invalid strings raise ValueError.
Python GUID: is GUID different from UUID?
GUID means globally unique identifier. UUID means universally unique identifier. In practice they describe the same 128-bit ID format—the hyphenated hex string you get from Python’s uuid module.
There is no separate guid module in the standard library. When documentation or APIs say “GUID” (common on Windows, in .NET, SQL Server, or Azure resource IDs), use uuid in Python:
import uuid
# "Generate a GUID" in Python = generate a UUID
guid_str = str(uuid.uuid4())
print(guid_str)
print(type(guid_str))You should see a string like a3f2c8d1-....-....-....-............ and <class 'str'>.
Parsing works the same whether the source calls it a GUID or UUID:
import uuid
raw = "550e8400-e29b-41d4-a716-446655440000"
wrapped = "{550e8400-e29b-41d4-a716-446655440000}" # common on Windows
print(uuid.UUID(raw))
print(uuid.UUID(wrapped))Python accepts the brace-wrapped form directly.
Some systems store GUIDs uppercase; Python’s UUID accepts either case. For JSON or REST APIs that expect a GUID field, str(uuid.uuid4()) is the usual value—same as for UUID.
Bottom line: searching for “python guid” or “python uuid” leads to the same code path: import uuid, then uuid4(), uuid5(), or UUID(string) as needed.
uuid1() vs uuid4(): which should you use?
| Function | How it generates UUID | Best for | Caution |
|---|---|---|---|
uuid1() |
Time, clock sequence, and node information | Time-ordered IDs | May expose machine/network-related data |
uuid4() |
Random UUID | General random unique IDs | Not naturally sortable by creation time |
Recommendation: use uuid4() unless you specifically need a time-based UUID. uuid1() embeds timestamp semantics related to Python datetime values, but it is not a drop-in replacement for datetime.now().
import uuid
print(uuid.uuid4())
print(uuid.uuid1())Generate name-based UUID with uuid3() and uuid5()
uuid3() and uuid5() are deterministic: same namespace + name → same UUID every time.
uuid3()— MD5 hash of namespace + nameuuid5()— SHA-1 hash (preferred for new name-based IDs)
import uuid
host = "www.example.com"
print(uuid.uuid5(uuid.NAMESPACE_DNS, host))
print(uuid.uuid5(uuid.NAMESPACE_DNS, host))Both lines print the same UUID. Use this for stable IDs from domain names, URLs, or canonical strings—not when you want a fresh random ID on each call.
for host in ["www.golinuxcloud.com", "stackoverflow.com"]:
print(uuid.uuid5(uuid.NAMESPACE_DNS, host))UUID namespaces in Python
Built-in namespaces for uuid3() / uuid5():
| Constant | Typical use |
|---|---|
uuid.NAMESPACE_DNS |
Domain names |
uuid.NAMESPACE_URL |
URLs |
uuid.NAMESPACE_OID |
ISO OID strings |
uuid.NAMESPACE_X500 |
X.500 distinguished names |
The namespace separates UUID spaces so the same name under different namespaces yields different UUIDs.
Generate short UUID in Python
A standard string UUID is 36 characters with hyphens; .hex is 32 characters without them. For shorter, URL-safe IDs, libraries such as shortuuid encode a UUID with a compact alphabet—install with pip install shortuuid when you need it.
Shorter or truncated IDs have higher collision risk than full 128-bit UUIDs. Do not truncate aggressively for primary keys without understanding the tradeoff.
import uuid
u = uuid.uuid4()
print(str(u))
print(u.hex)Python UUID command line
Generate UUIDs without a script:
python3 -m uuid
python3 -m uuid -u uuid4
python3 -m uuid -u uuid5 -n @dns -N example.comOn Python 3.13, python -m uuid supports uuid1, uuid3, uuid4, and uuid5. Newer Python versions extend CLI options as additional UUID versions are added to the stdlib.
UUID versions in Python 3.14 and newer
Python 3.14 added uuid6(), uuid7(), and uuid8() to the standard library:
| Function | Role (summary) |
|---|---|
uuid6() |
Time-based, alternative to uuid1() with better DB locality |
uuid7() |
Time-ordered UUID with Unix timestamp semantics |
uuid8() |
Custom UUID formats; not the default for random security-sensitive IDs |
These are not available on Python 3.13 (the version used to test this page). For most beginner and app code today, uuid4() remains the simplest random choice. Reach for 6/7/8 when you need their ordering or format properties on a supported Python version.
Common mistakes to avoid
- Using
uuid1()when privacy matters (host/time leakage). - Assuming short or truncated IDs match full UUID collision resistance.
- Using
uuid3()/uuid5()when you expect a new random ID each time. - Forgetting
str(uuid_obj)before JSON serialization. - Stripping hyphens when downstream tools expect the standard format.
- Treating UUIDs as sequential IDs—they are not guaranteed to sort by creation time (
uuid4()). - Using UUIDs as passwords or secrets without a proper security design.
Summary
Use uuid.uuid4() for most random UUIDs in Python. Use str(...) for the usual string form and .hex for 32 hex digits without hyphens. Use uuid5(namespace, name) for repeatable name-based UUIDs. Prefer uuid4() over uuid1() unless you need time-based IDs and accept privacy tradeoffs. Use shortuuid or similar only when shorter IDs are worth the collision tradeoff. python -m uuid generates UUIDs from the shell; Python 3.14+ adds uuid6(), uuid7(), and uuid8() for specialized cases.
See the Python uuid module documentation for full API details.

