Convert UUID to String in Python

In this post, we will see how to convert UUID to String in Python.

UUID in Python

UUID stands for Universally Unique Identifier. It is a 128 bits ID that is used to uniquely identify a resource in the digital world. This resource can be a document, user, file, and more. UUIDs also have an application in cryptography and hashing utilities. It is also termed GUID, which stands for Global Universal Identifier.

UUIDs are generated according to some specified universal standards set by the Internet Society to ensure the uniqueness of every ID so every resource has a distinct UUID.

In Python, we have the uuid module that can generate UUIDs. We can use functions from this module to create uuid objects. It supports different versions of UUID. We can generate UUID versions 1,3,4, and 5. Each version uses a different method to generate the unique ID.

Ways to convert UUID to string in Python

As discussed, UUIDs generated in Python belong to the uuid class. We will discuss how to convert UUID to String in Python.

Using the str() function to convert UUID to String in Python

The str() function is used to typecast different objects to a string. We can use this function to convert UUID to String in Python.

For example,

Output:

In the above example,

  • We create a uuid object using the uuid1() function.
  • The uuid1() function creates a UUID of version 1.
  • We convert this uuid to string in Python using the str() function.

Using the hex attribute to convert UUID to String in Python

A UUID is at times created from a string of 32-bit hexadecimal characters. The uuid class has a hex attribute, which returns the created UUID in a hexadecimal string format of 32-characters.

See the code below.

Output:

In the above example,

  • The hex attribute converts the uuid to string in Python.
  • The final result does not have any dashes.
  • A UUID without dashes is functionally identical to a full UUID. This happens because the position of dashes is fixed in a UUID.

Using the urn attribute to convert UUID to String in Python

A URN is a Uniform Resource Name that uniquely identifies a resource. It is defined from a UUID due to its uniqueness. We can return the URN for a uuid using its urn attribute in Python.

The returned URN is a string and can be viewed as a UUID by removing the unnecessary characters.

See the code below.

Output:

In the above example,

  • The urn attribute returns the URN from the uuid.
  • This URN contains the UUID prefixed with urn:uuid:.
  • This small part is trimmed using string slicing and the final UUID is printed as a string.

Conclusion

In this article, we discussed different methods to convert UUID to String in Python. We first discussed about UUID and the uuid module in Python. The most straightforward method uses the str() function to typecast and convert UUID to String in Python. We also discussed two properties of the uuid object called hex and urn that can help in this conversion.

That’s all about how to convert UUID to String in Python.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *