Python Programming
Coding Tutorials
GUID/UUID Generation
Advanced Python Concepts
Software Development

How to create a GUID/UUID in Python

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Python, the standard way to create a GUID or UUID is to use the built-in uuid module. Most application code that simply needs a unique identifier should use uuid.uuid4(), while other UUID versions exist for time-based or deterministic use cases.

GUID Versus UUID

In practice, GUID and UUID usually refer to the same kind of 128-bit identifier. Python’s module uses the UUID terminology, but the generated values are the sort of identifiers many systems also call GUIDs.

A UUID is typically displayed as a string such as:

text
550e8400-e29b-41d4-a716-446655440000

The Most Common Choice: uuid4

For most apps, random UUID version 4 is the simplest and safest default.

python
1import uuid
2
3value = uuid.uuid4()
4print(value)
5print(str(value))

This generates a new random UUID each time. It is a good fit for record IDs, request IDs, and general uniqueness where reproducibility is not required.

Other UUID Versions

Python also supports other versions.

uuid1

Time-based and partly node-based.

python
import uuid
print(uuid.uuid1())

This can embed timing and node-related information, so it is less appealing when you want to avoid exposing system details.

uuid3 And uuid5

Deterministic UUIDs based on a namespace and a name.

python
1import uuid
2
3print(uuid.uuid3(uuid.NAMESPACE_DNS, "example.com"))
4print(uuid.uuid5(uuid.NAMESPACE_DNS, "example.com"))

If you call these again with the same namespace and name, you get the same UUID again. That makes them useful when stable mapping matters.

Storing UUIDs As Strings

Many applications convert UUID objects to strings before storing them in text-based systems.

python
1import uuid
2
3identifier = str(uuid.uuid4())
4print(identifier)

If your database or framework has native UUID support, you may be able to store the UUID object or its binary form more efficiently. But string form is the most portable and common representation.

Convert Back From A String

If you already have a UUID string and want a UUID object again, parse it directly.

python
1import uuid
2
3text = "550e8400-e29b-41d4-a716-446655440000"
4value = uuid.UUID(text)
5print(value.version)

This is useful when validating or normalizing identifiers coming from APIs or config files.

Useful Representations

The UUID object can be rendered in more than one form. Besides the normal dashed string, you can access the hexadecimal representation with value.hex or the raw 16-byte value with value.bytes.

python
1import uuid
2
3value = uuid.uuid4()
4print(value.hex)
5print(value.bytes)

Those forms are helpful when integrating with binary protocols, compact storage formats, or systems that expect UUID text without dashes.

Which Version Should You Use

A practical rule is:

  • use uuid4() for ordinary unique IDs
  • use uuid5() when you need deterministic IDs from stable names
  • use uuid1() only if its time-based properties are intentionally useful

That keeps the choice simple and avoids overthinking the problem.

Common Pitfalls

The most common mistake is using uuid1() without realizing it is not just random noise. If privacy or predictability matters, uuid4() is usually the better default.

Another mistake is treating UUID strings as guaranteed secret tokens. They are identifiers, not authentication credentials.

A third issue is generating deterministic UUIDs with uuid3() or uuid5() and then being surprised that the same input gives the same result every time. That is the intended behavior.

Summary

  • Use Python’s built-in uuid module to generate GUIDs or UUIDs.
  • 'uuid.uuid4() is the normal default for random unique identifiers.'
  • 'uuid.uuid3() and uuid.uuid5() are deterministic for stable namespace-name pairs.'
  • Convert UUIDs to strings easily with str().
  • Choose the version based on whether you need randomness, determinism, or time-based behavior.

Course illustration
Course illustration

All Rights Reserved.