How to Generate a UUID in Python
The simplest way to generate a UUID in Python is uuid.uuid4(), which uses your operating system's cryptographically secure random number generator to produce a 128-bit identifier with virtually zero chance of collision. No installation, no configuration — just import the standard library module and call the function. The online uuid generator runs entirely client-side and produces cryptographically secure random identifiers.
import uuid
my_uuid = uuid.uuid4()
print(my_uuid)
# Output: 550e8400-e29b-41d4-a716-446655440000
Explanation
import uuidloads Python's built-in UUID module — nopip installrequired.uuid.uuid4()generates a random UUID (version 4) usingos.urandom()under the hood, which is cryptographically secure.print(my_uuid)calls the UUID object's string representation automatically, producing the standard 36-character hyphenated format.- The result is a
uuid.UUIDobject, not a plain string — callstr(my_uuid)explicitly when you need a string for concatenation or JSON serialization.
Convert a String to a UUID in Python
Sometimes you receive a UUID as a plain string — from a form field, a JSON payload, or a config file — and need to work with it as a proper uuid.UUID object again. Python's uuid module makes this a one-liner. Use the uuid v4 generator to generate a 122-bit random UUID instantly, with one-click clipboard copy.
import uuid
my_uuid = uuid.uuid4()
my_uuid_str = str(my_uuid)
# Convert the string back into a UUID object
same_uuid = uuid.UUID(my_uuid_str)
assert my_uuid == same_uuid
Explanation
uuid.UUID(my_uuid_str)parses any valid UUID string — with or without hyphens, uppercase or lowercase — into auuid.UUIDobject.- The constructor raises
ValueErrorif the string isn't a valid 32-character hex UUID, so wrap it in atry/exceptblock when parsing untrusted input. - Two
UUIDobjects compare equal with==if they represent the same identifier, regardless of how each was originally created. - Use this pattern to validate incoming UUIDs from API requests or database rows before trusting them.
Other Options for Generating UUIDs in Python
uuid4() covers most use cases, but Python's standard library also ships three other UUID generation methods for when you need a timestamp-based or deterministic identifier instead of a random one. The java uuid page covers UUID comparison, sorting, and storage in PostgreSQL via the JDBC driver.
import uuid
# Version 1 - timestamp + MAC address, sortable by creation time
uuid1_id = uuid.uuid1()
# Version 5 - deterministic, same namespace + name always produce the same UUID
uuid5_id = uuid.uuid5(uuid.NAMESPACE_DNS, 'example.com')
# Version 3 - like v5, but uses MD5 instead of SHA-1 (legacy, prefer v5)
uuid3_id = uuid.uuid3(uuid.NAMESPACE_DNS, 'example.com')
Explanation
uuid.uuid1()embeds the current timestamp and the machine's MAC address, so IDs generated on the same system sort chronologically — but it can leak hardware identifiers, so avoid it for public-facing IDs.uuid.uuid5(namespace, name)hashes a namespace UUID and a name string with SHA-1, producing the exact same UUID every time for the same inputs — useful for content-addressed identifiers.uuid.uuid3()works identically touuid5()but hashes with MD5; RFC 4122 recommendsuuid5()for new applications since SHA-1 is stronger.- None of these require external packages — all four UUID versions ship in Python's standard library.