In this post, we will see how to convert UUID to String in Python.
Table of Contents
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,
|
1 2 3 4 5 6 7 |
import uuid id1 = uuid.uuid1() print('Type', type(id1), 'UIID', id1) s = str(id1) print('Type', type(s), 'UIID', s) |
Output:
|
1 2 3 4 |
Type <class 'uuid.UUID'> UIID 60fc1510-9fdf-11ec-82ad-8c164555fad8 Type <class 'str'> UIID 60fc1510-9fdf-11ec-82ad-8c164555fad8 |
In the above example,
- We create a
uuidobject using theuuid1()function. - The
uuid1()function creates a UUID of version 1. - We convert this
uuidto string in Python using thestr()function.
Further reading:
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.
|
1 2 3 4 5 6 7 |
import uuid id1 = uuid.uuid1() print('Type', type(id1), 'UIID', id1) s = id1.hex print('Type', type(s), 'UIID', s) |
Output:
|
1 2 3 4 |
Type <class 'uuid.UUID'> UIID c7a099f0-9fdf-11ec-8ba9-8c164555fad8 Type <class 'str'> UIID c7a099f09fdf11ec8ba98c164555fad8 |
In the above example,
- The
hexattribute converts theuuidto 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.
|
1 2 3 4 5 6 7 8 |
import uuid id1 = uuid.uuid1() print('Type', type(id1), 'UIID', id1) s = id1.urn print('URN', s) print('Type', type(s), 'UIID', s[9:]) |
Output:
|
1 2 3 4 5 |
Type <class 'uuid.UUID'> UIID 0c858464-9fe0-11ec-8299-8c164555fad8 URN urn:uuid:0c858464-9fe0-11ec-8299-8c164555fad8 Type <class 'str'> UIID 0c858464-9fe0-11ec-8299-8c164555fad8 |
In the above example,
- The
urnattribute returns the URN from theuuid. - 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.