Python UUID to String: The One Line You Need (and the Surprising Formats Nobody Tells You About)
You’ve just generated a fresh UUID in Python. Now you try to save it to a database, include it in a JSON response, or share it in a URL —…
Python UUID to String: The One Line You Need (and the Surprising Formats Nobody Tells You About)

You’ve just generated a fresh UUID in Python.
Now you try to save it to a database, include it in a JSON response, or share it in a URL — and you immediately get a TypeError or a field that looks like gibberish.
UUIDs are everywhere. They identify users, orders, files, logs, and API resources. But Python’s uuid.UUID object isn’t a string. If you don’t convert it properly, you’ll end up with broken serializers, weird SQL insertions, or base64‑encoded IDs that look nothing like the classic 550e8400-e29b-41d4-a716-446655440000.
In this article I’ll show you the one line to convert a UUID to a string — plus the less‑known formats that solve real problems in web apps, databases, and distributed systems. I’ll also point you to a free generator I use when I need to test the output instantly.
The 1‑second answer: just use str()
The most common conversion is also the simplest:
import uuid
my_uuid = uuid.uuid4()
uuid_string = str(my_uuid)
print(uuid_string)
# Output: 3d5e9c68-8f2a-4b9e-a7e1-2c8f5e4b7a1d
That’s the standard 36‑character hex representation with hyphens. It works everywhere: PostgreSQL’s UUID column, JSON strings, URL parameters, log messages, or just printing to a terminal.
But — the moment you need to store a UUID in a fixed‑width binary column, embed it inside a compact QR code, or pass it through an API that expects a raw 16‑byte value, str() alone doesn’t help. You need other representations.
The three UUID formats nobody explains in the docs
1. Hex string (without hyphens): uuid.hex
If you want a compact, readable identifier without dashes — perfect for filenames or URL slugs — use the .hex property:
print(my_uuid.hex)
# Output: 3d5e9c688f2a4b9ea7e12c8f5e4b7a1d
It’s still safe to embed in a URL path segment, and it’s 32 characters instead of 36. Many payment gateways and external APIs prefer this format.
2. Bytes (raw 16 bytes): uuid.bytes
When you need to store a UUID in a binary column (e.g., BINARY(16) in MySQL, bytea in PostgreSQL with some optimizations), you want the raw bytes:
print(my_uuid.bytes)
# Output: b'=\x9e\xc6h\x8f*K\x9e\xa7\xe1,\x8f^Kz\x1d'
That’s a bytes object of length 16. It’s not human‑readable, but it’s efficient. Many ORMs like SQLAlchemy can work directly with uuid.UUID objects, but sometimes you have to pass raw bytes to a low‑level driver.
3. Base64 (or base64url): base64 module
This one is gold when you need to send UUIDs over the wire without worrying about URL‑unsafe characters. The standard base64 encoding of a UUID’s 16 bytes gives you a 24‑character string:
import base64
uuid_b64 = base64.urlsafe_b64encode(my_uuid.bytes).rstrip(b'=').decode('ascii')
print(uuid_b64)
# Output: PV2caI8qS56n4SyPXkt6HQ
The .rstrip(b'=') removes the padding = characters, making it fully URL‑safe. This is what you’d use in a REST API that needs short, opaque identifiers — Stripe, Auth0, and many others use exactly this technique.
You can then convert back with:
uuid_from_b64 = uuid.UUID(bytes=base64.urlsafe_b64decode(uuid_b64 + '=='))
Why the conversion matters in real applications
Over the years I’ve seen three common situations where getting the conversion wrong caused silent bugs:
-
JSON serialization
json.dumps({id: my_uuid})fails with aTypeError. You must convert the UUID to a string first. A clean pattern is to define a custom JSON encoder that handlesUUIDobjects automatically. -
Database ORMs Django’s ORM accepts
uuid.UUIDfor aUUIDField. But if you’re using raw SQL or a lightweight driver likesqlite3, you’ll need to pass a string or bytes explicitly. I once spent an afternoon debugging a query that was insertingUUID('...')as a literal string because I forgot to convert. -
Logging and debugging When a log line prints a
UUIDobject, it showsUUID('3d5e9c68-...'). That’s noisy. I always usestr(uuid_obj)in f‑strings to keep logs clean.
When you just need to generate and convert in the browser
Sometimes I’m designing a database schema or testing an API, and I don’t want to open a Python shell. For that, I use the free Devstoolsbox UUID Generator. It creates UUIDs (and ULIDs) instantly and shows the string, hex, bytes, and base64 representations side by side. No account needed, everything runs in the browser. It’s especially handy for comparing the output of different formats before you write the Python code.
A reusable converter function
If you’re dealing with multiple formats in the same project, a small utility helps:
import uuid
import base64
def uuid_to_formats(u: uuid.UUID):
return {
"string": str(u),
"hex": u.hex,
"bytes": u.bytes,
"base64url": base64.urlsafe_b64encode(u.bytes).rstrip(b'=').decode('ascii')
}
Final thought
Converting a Python UUID to a string really is one line. But the moment you step outside the standard 36‑character format, you unlock cleaner URLs, smaller storage, and better interoperability with external systems.
Now, if you’ll excuse me, I have a few dozen UUIDs to generate for a new API — and I’ll probably use the online tool for a quick sanity check.
Have your own UUID tricks or horror stories? Drop a comment below. And if you found this useful, a clap or two would make my day.
메타데이터
- post_id
- d117d2639f64
- slug
- python-uuid-to-string-the-one-line-you-need-and-the-surprising-formats-nobody-tells-you-about-d117d2639f64
- url
- https://medium.com/@kouadiomathias64/python-uuid-to-string-the-one-line-you-need-and-the-surprising-formats-nobody-tells-you-about-d117d2639f64
- canonical_url
- https://medium.com/@kouadiomathias64/python-uuid-to-string-the-one-line-you-need-and-the-surprising-formats-nobody-tells-you-about-d117d2639f64
- author_url
- https://medium.com/@kouadiomathias64
- status
- ok
- fetched_at
- 2026-08-25 00:49:16