How to Generate Random UUID in Python
To generate a random UUID in Python, import the uuid module and call uuid.uuid4(): import uuid then str(uuid.uuid4()) returns a string like '123e4567-e89b-12d3-a456-426614174000'. It is the default choice for random unique IDs. For bulk or v7 UUIDs without code, try our free UUID generator.
The One-Line Answer: A Python uuid4 Example
The shortest python uuid4 example is two lines. First import the module with import uuid, then call str(uuid.uuid4()) to get a string such as '123e4567-e89b-12d3-a456-426614174000'. The str() call matters because uuid.uuid4() on its own returns a UUID object, not a string, and most uses, such as storing the value in a database, putting it in a JSON response, or using it as a filename, need the string form. You can also keep the object and convert it later, since str() works on the object at any point. A UUID is 128 bits, shown as 36 characters with hyphens, or 32 characters without them, and uuid4 fills 122 of those bits with random data, which is why two calls practically never return the same value. If you only need the value once, the two lines above are the whole answer. If you need it inside a function, wrap it: define a function that returns str(uuid.uuid4()) and call it wherever an ID is needed. The uuid module is part of the Python standard library, so there is nothing to install and no third-party package to keep updated. That is one reason uuid4 is the first answer in almost every Python project that needs a unique ID, from web apps assigning order numbers to scripts naming temporary files.
uuid1 vs uuid4 in Python
The uuid1 vs uuid4 python question comes down to how the value is built. uuid4 is random, using 122 random bits, and it reveals nothing about where or when it was created. uuid1 is built from the current time and the machine's MAC address, which means it can leak the machine and the time it was generated, so avoid it for public IDs such as URLs, API keys, or anything a stranger will see. uuid1 does have one advantage: values generated close together sort roughly in time order, because the timestamp is part of the value, which can help when IDs are used as database keys and insertion order matters. uuid4 has no ordering at all, since random values sort randomly. For most projects the choice is simple: use uuid4 unless you have a specific reason to need time ordering, and if you do need time ordering, look at version 7 instead of uuid1, because v7 is designed for that job without exposing a MAC address. The code for uuid1 looks the same, str(uuid.uuid1()), so switching between them is easy, but the privacy difference is real. Never use uuid1 for session tokens, password reset links, or public resource IDs, where the leaked timestamp and machine address could help an attacker. When in doubt, uuid4 is the safe default, and it is the version this guide recommends first.
How to Generate a Unique ID in Python
Knowing how to generate unique id in python means knowing which UUID version fits the job. uuid4 is the default for a random unique ID, and it suits most cases: database primary keys, request IDs, file names, and test data. uuid5 is deterministic: the same namespace and name always give the same UUID, so uuid.uuid5(uuid.NAMESPACE_DNS, 'example.com') returns the same value every time you run it. That makes uuid5 the right choice when you need a stable ID derived from a name, such as turning a username or a URL into an ID that never changes. uuid3 is the same idea with MD5 instead of SHA-1, so it is also deterministic but built on an older hash, and uuid5 is generally preferred over uuid3 for new code. A UUID is 128 bits, 36 characters with hyphens and 32 without, so it fits comfortably in a database column or a URL, and its fixed length makes it easy to validate: a string that is 36 characters long with hyphens in the right places is at least shaped like a UUID. If you need an ID that is short, a UUID is not the tool: its strength is uniqueness across machines and time, not brevity. For short IDs, a counter or a short random string is a better fit, but you lose the guarantee that two machines will never collide.
Generating UUIDs in Bulk
Bulk generation is a one-liner with a list comprehension: [str(uuid.uuid4()) for _ in range(10)] returns a list of ten fresh random UUIDs. Change the 10 to however many you need, and the same pattern works for uuid1 or uuid5, though bulk uuid5 with the same name would return the same value ten times, so vary the name in that case. Common uses for bulk generation include seeding a test database with sample rows, creating placeholder IDs for a batch import, and generating a set of invite codes or API keys. If you need hundreds of UUIDs and do not want to write code at all, our free UUID generator generates v4 and v7 UUIDs in bulk up to 500 at a time, with no signup. When you generate in bulk, check for duplicates if the list is very large: with 122 random bits, a collision is so unlikely that you will never see one in practice, but a quick set() check costs nothing and proves the point. Storing millions of UUIDs as text takes real space, since each one is 36 characters, so a million UUIDs as text is about 36 megabytes before any other columns, and if you want to compare storage units while planning a table, our gigabytes to megabytes converter makes the conversion quick. For most apps, storing the 16-byte binary form instead of the 36-character text saves space, and the uuid module supports that through the bytes attribute of a UUID object.
UUID Format: 128 Bits, 36 Characters
A UUID is 128 bits, written as 36 characters with hyphens, or 32 characters without them. The standard text form has five groups separated by hyphens, in the pattern 8-4-4-4-12 characters, such as '123e4567-e89b-12d3-a456-426614174000'. The hyphens are only separators: remove them and you have the same 128-bit value as 32 hexadecimal characters. The version number sits in the first character of the third group, so a uuid4 value has a 4 there, a uuid1 value has a 1, and a uuid5 value has a 5, which lets you tell the version at a glance. The variant bits sit near the start of the fourth group and are usually 8, 9, a, or b. Under the hood, a UUID is just 128 bits, and if you want to see how text maps to bits, our text to binary converter shows the binary form of any text you enter. When you store a UUID, the text form is the easy choice because it is readable and works everywhere, while the binary form is the compact choice because it takes 16 bytes instead of 36. When you validate a UUID, check the length and the hyphen positions first, then check that every character is a hexadecimal digit or a hyphen. Most databases and frameworks accept the text form directly, so you rarely need to convert between forms by hand.
When to Use Each UUID Version
Use uuid4 when you need a random unique ID and nothing else matters: it is the default, it leaks nothing, and it is the version most Python code uses. Use uuid1 only when you need time ordering and you accept that the value exposes the generation time and the machine's MAC address, and never for public IDs. Use uuid5 when you need the same input to give the same ID every time, such as deriving a stable ID from a domain name or a username, and use uuid3 only when you need the same deterministic behavior with MD5 for compatibility with older code. For time-ordered IDs, version 7 is the modern answer: v7 UUIDs sort by creation time, which keeps database inserts fast, without exposing a MAC address the way uuid1 does. There is no uuid.uuid7() in the Python standard library as of Python 3.13, so v7 time-ordered UUIDs need a third-party package in pure Python, but our generator produces them. Our free UUID generator produces v4 and v7 UUIDs in bulk up to 500, so you can get time-ordered IDs without installing anything. If you are learning what a UUID is and how it is generated, the version number in the third group is the fastest way to read any UUID you meet. Pick the version from the job, not from habit: random for most IDs, deterministic for stable names, and time-ordered when sorting matters.
Frequently Asked Questions
How do I generate a random UUID in Python? Import the uuid module and call str(uuid.uuid4()), for example import uuid then str(uuid.uuid4()), which returns a string like '123e4567-e89b-12d3-a456-426614174000'. What is the difference between uuid1 and uuid4? uuid4 is random with 122 random bits and leaks nothing, while uuid1 is built from the time and the MAC address and can leak the machine and time, so avoid uuid1 for public IDs. How do I generate a unique ID in Python? Use str(uuid.uuid4()) for a random ID, or uuid.uuid5(uuid.NAMESPACE_DNS, 'example.com') when the same name must always give the same ID. How do I generate many UUIDs at once? Use a list comprehension, [str(uuid.uuid4()) for _ in range(10)], or our free UUID generator for up to 500 at a time. How long is a UUID? A UUID is 128 bits, 36 characters with hyphens and 32 without. Does Python have uuid7? Not in the standard library as of Python 3.13, so v7 needs a third-party package in pure Python, though our generator produces v7 UUIDs. Can two uuid4 values collide? Practically never: 122 random bits make a collision far less likely than a hardware failure. For a related Python task, see our guide to converting Markdown to HTML in Python.