UUID.hex / int / bytes / bytes_le / urn

int is the only value a UUID really stores; hex, bytes, bytes_le and urn are computed from it on every access. hex is str(u) without hyphens; bytes_le swaps the first three fields into the little-endian order Microsoft GUIDs use.

UUID attributesPython 2.5+Live demo
Common call
u.hex
Returns
'886313e13b8a53729b900c9aee199e5d' (32 lowercase digits)
Replaces
str(u).replace('-', '')
Watch out
bytes_le is not bytes reversed: only the first 8 bytes are reordered
u.hex · u.int · u.bytes · u.bytes_le · u.urn
→ str / int / bytes / bytes / str

Demo

Live evaluation
str(), hex and urn of the UUID you type.
Try:
Inputs
textstra UUID string
Code
import uuid
u = uuid.UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d')
(str(u), u.hex, u.urn)
Result
('886313e1-3b8a-5372-9b90-0c9aee199e5d', '886313e13b8a53729b900c9aee199e5d', 'urn:uuid:886313e1-3b8a-5372-9b90-0c9aee199e5d')

In the 00 01 02 … 0f case, bytes counts up while bytes_le starts 03 02 01 00, 05 04, 07 06 and then continues unchanged from 08: only the three time fields are byte-swapped. bytes are shown as Python prints them, so 0x09, 0x0a and 0x0d appear as \t, \n and \r, and printable bytes as their ASCII character.

Attributes

AttributeTypeMeaning
hexstr32 lowercase hex digits, no hyphens ('%032x' % u.int).
intintThe UUID as an int, 0 to 2**128 - 1. A __slots__ attribute, the one value that is stored.
bytesbytes16 bytes, big-endian: u.int.to_bytes(16).
bytes_lebytesLike bytes, but time_low (4 bytes), time_mid (2) and time_hi_version (2) each reversed.
urnstr'urn:uuid:' + str(u).

Common patterns

Compact storage
16 bytes in a BLOB/BINARY(16) column instead of a 36-character string.
import uuid
row_key = some_uuid.bytes
loaded = uuid.UUID(bytes=row_key)
Read a Windows GUID
Binary GUIDs from Windows APIs and some databases use the bytes_le layout.
import uuid
guid = uuid.UUID(bytes_le=raw16)
Hex for file names and URLs
No hyphens, fixed length, safe everywhere.
import uuid
filename = f'{uuid.uuid4().hex}.png'

Examples

1. hex: no hyphens
import uuid uuid.UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d').hex
Returns
'886313e13b8a53729b900c9aee199e5d'
2. int: the 128-bit value
import uuid uuid.UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d').int
Returns
181289448026289383154478846676280385117
3. urn
import uuid uuid.UUID(int=1).urn
Returns
'urn:uuid:00000000-0000-0000-0000-000000000001'
4. bytes round trip
import uuid u = uuid.uuid4() uuid.UUID(bytes=u.bytes) == u
Returns
True
5. bytes_le reorders 8 bytes
import uuid uuid.UUID('00010203-0405-0607-0809-0a0b0c0d0e0f').bytes_le.hex()
Returns
'030201000504070608090a0b0c0d0e0f'
6. Always 32 hex digits
import uuid len(uuid.uuid4().hex)
Returns
32
7. Upper-case hex
import uuid uuid.UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d').hex.upper()
Returns
'886313E13B8A53729B900C9AEE199E5D'

Pitfalls

1. Mixing up bytes and bytes_le
Reading GUID-ordered bytes with bytes= (or the reverse) gives a different UUID, with no error.
bytes=
import uuid
uuid.UUID(bytes=bytes.fromhex('78563412341278561234567812345678'))
UUID('78563412-3412-7856-1234-567812345678')
bytes_le=
import uuid
uuid.UUID(bytes_le=bytes.fromhex('78563412341278561234567812345678'))
UUID('12345678-1234-5678-1234-567812345678')
2. Passing hex text as bytes=
bytes= wants the 16 raw bytes. The 32-character hex text encoded to bytes is 32 bytes long.
encoded text
import uuid
uuid.UUID(bytes='12345678123456781234567812345678'.encode())
ValueError: bytes is not a 16-char string
bytes.fromhex
import uuid
uuid.UUID(bytes=bytes.fromhex('12345678123456781234567812345678'))
UUID('12345678-1234-5678-1234-567812345678')

When to use

Use it
  • hex: compact text for file names, cache keys and URLs
  • bytes: binary columns and network protocols
  • int: arithmetic, bit tests, integer database columns
  • bytes_le: interop with Windows GUID structures
Reach for something else
  • Showing a UUID to people → str(u), the standard hyphenated form
  • Parsing → construct with UUID(hex=..., bytes=..., int=...) instead of slicing these values

Notes

CPython impl
Properties in Lib/uuid.py: hex is '%032x' % self.int, bytes is self.int.to_bytes(16), bytes_le slices and reverses bytes[0:4], [4:6] and [6:8], urn is 'urn:uuid:' + str(self)
Read-only
All five are read-only: assignment raises TypeError: UUID objects are immutable
Case
hex and str() are always lowercase; parsing accepts either case

FAQ

u.hex returns the 32 lowercase hex digits without hyphens, e.g. uuid.uuid4().hex.