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?

Related Posts

  • 19 May

    Remove First and Last Character of String in Python

    Table of ContentsHow to Remove First and Last Character of string in python?Remove First and Last Character of string Using For LoopRemove First and Last Character of string Using SlicingRemove First and Last word of string in PythonConclusionWas this post helpful? We use strings in python to handle text data. For processing the data, we […]

  • 19 May

    Remove HTML Tags From String in Python

    Table of ContentsRemove HTML tags from string in python Using Regular ExpressionsRemove HTML tags from string in python Using the lxml ModuleRemove HTML tags from string in python Using the Beautifulsoup ModuleConclusionWas this post helpful? While collecting data, we often need to process texts with HTML tags. In this article, we will discuss different ways […]

  • 19 May

    Remove xa0 from String in Python

    Table of ContentsHow to remove xa0 from string in python?Using the decode() function to remove xa0 from string in python.Using the re library to remove xa0 from string in python.Using the normalize() function from the unicodedata library to remove xa0 from string in python.Using the get_text() function from the BeautifulSoup library to remove xa0 from […]

  • 18 May

    Convert bool to String in Python

    Table of ContentsUsing the str() function to convert bool to string in Python.Using string formatting with the help of the % operator to convert bool to string in Python.Using string formatting with the help of the format() function to convert bool to string in Python.Using f-strings to convert bool to string in Python.Was this post […]

  • 18 May

    How to Replace Word in String in Python

    Table of ContentsUsing the replace() Function to replace word in string in PythonUsing the re.Sub() Function to replace word in string in PythonUsing the re.subn() function to replace word in string in PythonConclusionWas this post helpful? In this tutorial, we will to how to replace word in string in Python. Strings represent a sequence of […]

  • 18 May

    Escape Curly Brace in f-String in Python

    Table of ContentsUse of the f-string in PythonHow to escape curly brace in f-string in PythonConclusionWas this post helpful? In this tutorial, we will see how to escape curly brace in f-string in Python. Use of the f-string in Python In Python, we can format strings using different methods to get the final result in […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.