uuid.UUID
Exactly one of hex, bytes, bytes_le, fields or int. The hex string may have braces, a urn:uuid: prefix, hyphens anywhere and any case; what is left must be 32 characters that int(..., 16) accepts. version= overwrites the version and variant bits.
Demo
import uuid uuid.UUID('{12345678-1234-5678-1234-567812345678}')
The parser does not check where hyphens are: it deletes all of them. It also does not strip whitespace, so a trailing space makes 33 characters and "badly formed hexadecimal UUID string". With exactly 32 characters the final step is int(text, 16), so a non-hex digit fails with int's message instead. In the compare tab the two spellings are equal UUIDs, but str(a) only equals the lowercase canonical text.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| hex | str | no (None) | 32 hex digits; 'urn:' and 'uuid:' are removed anywhere, '{' and '}' stripped from the ends, every '-' removed. Positional. |
| bytes | bytes | no (None) | 16 bytes, big-endian (the network order of the six fields). |
| bytes_le | bytes | no (None) | 16 bytes with the first three fields little-endian (the Microsoft GUID layout). |
| fields | tuple[int, ...] | no (None) | (time_low, time_mid, time_hi_version, clock_seq_hi_variant, clock_seq_low, node): 32, 16, 16, 8, 8 and 48 bits. |
| int | int | no (None) | The whole UUID as an int from 0 to 2**128 - 1. |
| version | int | no (None) | 1 to 5 in 3.13: sets the variant to RFC 4122 and the version nibble, overriding those bits of the input. |
| is_safe | SafeUUID | no (SafeUUID.unknown) | Keyword-only; stored as the is_safe attribute (3.7+). |
Return value
uuid.UUID — An immutable, hashable UUID holding one 128-bit integer.
Attributes
| Attribute | Type | Meaning |
|---|---|---|
| uuid.int_ | type | Module-level alias of the built-in int. The class body defines properties named int and bytes, so uuid.py keeps int_ and bytes_ to reach the built-ins. Not documented; do not use it. |
| uuid.bytes_ | type | Module-level alias of the built-in bytes, for the same reason. |
Common patterns
import uuid def parse_uuid(text): try: return uuid.UUID(text.strip()) except ValueError: return None
import uuid def is_canonical(text): try: return str(uuid.UUID(text)) == text except ValueError: return False
import uuid by_id = {uuid.UUID(row['id']): row for row in rows} by_id[uuid.UUID(request_id)]
Examples
Pitfalls
import uuid uuid.UUID(b'12345678123456781234567812345678')
import uuid uuid.UUID(b'12345678123456781234567812345678'.decode())
import uuid uuid.UUID()
import uuid uuid.UUID(int=0)
import uuid uuid.UUID('12345678-1234-5678-1234-567812345678\n')
import uuid uuid.UUID('12345678-1234-5678-1234-567812345678\n'.strip())
When to use
- Parsing and validating UUIDs from text, bytes or database ints
- Storing IDs as objects that compare, sort and hash by value
- Making a new ID → uuid4() (or uuid5() from a name)
- Strict format checking → compare with str(uuid.UUID(s)), since the parser is lenient
Notes
FAQ
uuid.UUID(s). It accepts the canonical form, braces, a urn:uuid: prefix, missing hyphens and upper case, and raises ValueError otherwise.