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

  • 25 December

    Encode String to UTF-8 in Python

    Table of ContentsEncode String to UTF-8 in PythonUsing the encode() functionUsing the codecs.encode() functionConclusion The UTF-8 encoding is used by default in Python and represents 8-bit Unicode values. The upgrade to Python 3 saw a major change in using ASCII characters to Unicode characters by default for strings. Encode String to UTF-8 in Python This […]

  • 23 December

    How To Do Line Continuation in Python

    Table of ContentsUsing Backslash (\) OperatorLine Continuation in StringLine Continuation in Number ExpressionUsing Parentheses ()Line Continuation in StringLine Continuation in Number Expression Using Backslash (\) Operator We can use \ operator for line continuation in string and number expression as follows. Line Continuation in String To do line continuation in Python Strings: Use the backslash […]

  • 16 December

    Convert List to Comma Separated String in Python

    Table of ContentsUse .join() MethodUse .join() with map() MethodUse .join() with List Comprehension Use .join() Method To convert a list to a comma separated string in Python, use the .join() method. join() method takes all elements in an iterable and joins them into one string with delimiter as separator. [crayon-63da5d2c329fc876194827/] [crayon-63da5d2c329ff160581536/] First, we created a […]

  • 09 December

    Count Occurrences of Character in String in Python

    Table of ContentsUsing count() MethodUsing Naive for loopUsing List ComprehensionUsing re.findall() MethodUsing for loop with re.finditer() MethodUse the lambda Function with map()Use collections.Counter() Method Using count() Method Use String’s count() method to count occurrences of character in String in Python e.g. my_string.count(character). count() method returns number of occurrences of the character in String. [crayon-63da5d2c32b6b001477733/] We […]

  • 08 December

    Find Character in String in Python

    Table of ContentsUsing find() MethodUsing index() MethodUsing for Loop with re.finditer() MethodUsing list comprehension Using find() Method To find the character in a string in Python: Use the find() method to find the index of the first occurrence of the supplied character in the input String. Use an if statement to check if the returned […]

  • 05 December

    Remove Substring from String in Python

    Table of ContentsUse replace() MethodUse the split() Method with for loopUse split() with join() MethodUse re.sub() Method Use replace() Method To eliminate a substring from string in Python: Define and initiate my_string. Declare substring to remove it from my_string. Use replace() method to remove substring from string. e.g. my_string = my_string.replace(substring, "") [crayon-63da5d2c32eba494964185/] The code […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.