Faster, better AI-powered code reviews. Start your free trial!  
Faster, better AI-powered code reviews.
Start your free trial!

Get high quality AI code reviews

Python: Converting UUIDs to Strings

Table of Contents

Universally unique identifiers (UUIDs) are 128-bit numbers used to uniquely identify information in systems. In Python, UUIDs are generated and handled as objects, but there are times you may need to work with the string representation of a UUID instead. This article will examine the steps and methods for converting UUIDs to strings in Python.

UUIDs are defined by RFC 4122 as 128-bit identifiers intended to have a high likelihood of uniqueness across space and time. They are commonly used in distributed systems, databases, and network protocols to assign unique identifiers without a central authority.

In Python, UUID objects are handled using the uuid module in the standard library. However, you may need to convert UUIDs to string representations for tasks like storage, display, HTTP requests, and more. The uuid module provides several attributes and methods to convert the UUID object to different string formats.

This article will demonstrate the various techniques to convert between the UUID object and string representations in Python.

UUID Module Basics

The uuid module contains functions to generate new UUID objects as well as methods to convert between different representations. Let’s first go over the essentials of generating UUIDs and the properties exposed on the UUID object.

Generating UUIDs

The uuid module contains functions to generate UUID objects such as:

  • uuid1() – Generates a UUID from the host ID, sequence number, and current time.
  • uuid3() – Creates a UUID based on the MD5 hash of a namespace UUID and name.
  • uuid4() – Generates a random UUID.
  • uuid5() – Creates a UUID using the SHA-1 hash of a namespace UUID and name.

For example:

import uuid

my_uuid = uuid.uuid4()
print(my_uuid)
# 5d3b2aee-552f-4a91-b56e-5b1b87b5c3f6 

Each function returns a UUID object with the standard fields and properties.

UUID Properties

The UUID object has several attributes and methods to access the value in different representations:

  • hex: Returns hexadecimal string form of UUID.
  • int: Returns integer representation of UUID.
  • urn: Returns UUID as a URN string.
  • bytes: Returns UUID as a 16-byte string.
  • fields: Returns tuple of UUID fields.
  • variant: Returns UUID variant (RFC 4122 defines variants).

We’ll focus primarily on hex, urn, and str for string conversions.

String Conversion

The simplest way to convert a UUID to a string is using the str() function:

uuid_str = str(my_uuid)
print(uuid_str)
# 5d3b2aee-552f-4a91-b56e-5b1b87b5c3f6

However, this is not the most efficient method. The uuid module provides better options which we’ll explore next.

Using the UUID hex Attribute

The hex attribute returns the string value of a UUID as a 32-character hexadecimal string. This is one of the most compact string representations.

Format

The format of the hex string returned is:

xxxxxxxxxxxxxxxxxxxxxxxxxxxx

Where each x represents a hexadecimal digit (0-9 or a-f).

Some examples:

5d3b2aee552f4a91b56e5b1b87b5c3f6
cbbd99de518b4ad7b6cf7636e582c3cf

Example

To use the hex attribute:

my_uuid = uuid.uuid4()

uuid_hex = my_uuid.hex 
print(uuid_hex)
# 5d3b2aee552f4a91b56e5b1b87b5c3f6

This converts the UUID to a 32-character string containing only hexadecimal digits.

Use Cases

The hex string representation is useful in some cases:

  • Compact representation without hyphens
  • Efficient for storage in databases and URLs
  • Can easily convert to/from bytes using hex encoding

Since it’s a compact format, it can be used when storage space is a concern such as databases. The lack of hyphens also makes it URL friendly.

Using the UUID URN Attribute

URN stands for Uniform Resource Name, and is a string identifier standardized by IETF RFC 8141. The urn attribute returns the UUID as a URN string.

Format

The URN representation of a UUID is:

urn:uuid:<uuid-value>

For example:

urn:uuid:5d3b2aee-552f-4a91-b56e-5b1b87b5c3f6

Where <uuid-value> is the standard UUID string with hyphens.

Example

To use the urn attribute:

my_uuid = uuid.uuid4()

uuid_urn = my_uuid.urn
print(uuid_urn) 
# urn:uuid:5d3b2aee-552f-4a91-b56e-5b1b87b5c3f6

This prefixes the UUID string with the urn:uuid: scheme.

Use Cases

The URN representation is useful in some cases:

  • Systems that recognize and handle the urn:uuid: scheme
  • Retrieving UUIDs from a URN resolver/registry
  • Clear that value is a UUID and not some other identifier

Since URNs are a standard identifier type, this can be used when interacting with other systems that understand the URN format.

Direct String Conversion

For most general use cases, converting a UUID to a standard dashed-string is the simplest option using the str() function.

str() Function

Directly calling str() on the UUID object returns the 36-character hyphenated string:

str(uuid_obj)

Example

import uuid

my_uuid = uuid.uuid4()

uuid_str = str(my_uuid)
print(uuid_str)
# 5d3b2aee-552f-4a91-b56e-5b1b87b5c3f6  

Use Cases

  • General string representation for display or logging
  • Sending UUIDs in HTTP APIs as request/response bodies
  • Works universally across languages and systems

The hyphenated string format is the most interoperable way of representing UUIDs across platforms and languages. It’s the safest option if you just need a string version of the UUID.

Conclusion

UUIDs are generated as objects in Python, but can be converted to different string representations using attributes like hex and urn or the str() function.

  • The hex attribute provides a compact string without hyphens, useful for efficient storage and transfer.
  • The urn attribute includes the standard urn:uuid: scheme prefix, for systems that understand that format.
  • str() returns the familiar dashed-string format, the most universally recognized UUID string representation.

The techniques covered offer flexibility for working with UUIDs as strings in Python systems. By understanding the available options, you can choose the most appropriate string format for your specific use case.

The UUID object also contains additional attributes like bytes and fields for alternate representations when needed. But for general interoperability, the standardized dashed-string from str() is recommended.

Picture of Nisha Kumari

Nisha Kumari

Nisha Kumari, a Founding Engineer at Bito, brings a comprehensive background in software engineering, specializing in Java/J2EE, PHP, HTML, CSS, JavaScript, and web development. Her career highlights include significant roles at Accenture, where she led end-to-end project deliveries and application maintenance, and at PubMatic, where she honed her skills in online advertising and optimization. Nisha's expertise spans across SAP HANA development, project management, and technical specification, making her a versatile and skilled contributor to the tech industry.

Written by developers for developers

This article was handcrafted with by the Bito team.

Latest posts

Mastering Python’s writelines() Function for Efficient File Writing | A Comprehensive Guide

Understanding the Difference Between == and === in JavaScript – A Comprehensive Guide

Compare Two Strings in JavaScript: A Detailed Guide for Efficient String Comparison

Exploring the Distinctions: == vs equals() in Java Programming

Understanding Matplotlib Inline in Python: A Comprehensive Guide for Visualizations

Top posts

Mastering Python’s writelines() Function for Efficient File Writing | A Comprehensive Guide

Understanding the Difference Between == and === in JavaScript – A Comprehensive Guide

Compare Two Strings in JavaScript: A Detailed Guide for Efficient String Comparison

Exploring the Distinctions: == vs equals() in Java Programming

Understanding Matplotlib Inline in Python: A Comprehensive Guide for Visualizations

Get Bito for IDE of your choice