Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is UUID?
2.1.
Structure of UUID
3.
Generating Python UUID
4.
Advantages and Disadvantages of UUID
4.1.
Advantages of UUID
4.2.
Disadvantages of UUID
5.
Frequently Asked Questions
5.1.
What are the different versions of UUIDs?
5.2.
What is the use of UUIDs?
5.3.
What are the hashing algorithms used by UUID versions 3 and 5?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Python UUID

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Using any ID to identify different objects is important when creating or managing databases. There might be cases where the information of two objects can be similar, so these IDs help in those cases. One of the common is UUIDs. UUID is a 36-character long alphanumeric string used as a unique number to identify information.

Python UUID

This blog will help you understand Python UUIDs. We can generate many different versions of UUIDs according to our requirements. We will learn about these versions in detail in the following blog. Also, we will understand their syntaxes with the help of some examples.

Also see, Swapcase in Python and Convert String to List Python.

What is UUID?

UUID is an abbreviation for universally unique identifier. It is a 128-bit long unique ID used to identify information in computer systems. It is also known as a Globally Unique IDentifier (GUID).

To generate a unique identifier, you should follow the standard procedure. Although, the chances that the ID generated by the user every time is unique are not 100% but are near 100%.

In Python, with the help of the Python UUID module, we can generate different types of UUIDs. The following are the different versions of the module:

  • UUID1:- it is used to generate a unique ID using date-time and the MAC address.
     
  • UUID2:- it also generates the unique number using date-time and the MAC address. Although RFC reserves the UUID2 version for “DCE security UUIDs”.
     
  • UUID4:- it randomly generates UUIDs.
     
  • UUID3 and UUID5:- It generates UUIDs by hashing a namespace identifier and name. The UUID3 uses the MD5 hashing algorithm, whereas UUID5 uses the SHA-1 algorithm.

See more, Python for Data Science

Structure of UUID

The UUID contains hexadecimal digits, which means it includes numbers from 1 to 9 and alphabets from A to F. The structure of Python UUID can be represented as 8-4-4-4-12, containing a total of 32 characters.

Let’s discuss briefly the structure of Python UUID.

UUID = time_low "-" time_mid "-"time_high_and_version "-"clock_seq_and_reserved_And_clock_seq_low"-" Node.

 

Following are the parameters used in the structure of UUID

  • time_low: The first 8 characters or 32 bits of the ID are represented by “time_low”. It gives the low 32 bits of the time.
     
  • time_mid: The following 4 characters or 16 bits of the ID are represented by “time_mid”. It gives the mid 16 bits of the time.
     
  • time_high_and_version: The following 4 characters or 16 bits of the ID are represented by “time_high_and_version”. It gives a 4-bit "version" in the most significant bits, followed by the high 12 bits of the time.
     
  • clock_seq_and_reserved_And_clock_seq_low: It represents the next 16 bits of the ID giving the 1-3 bit variant in the most significant bits and 13 to 15 bits of clock sequence.
     
  • Node: It represents the last 48 bits of the Python ID.
     

An example of a Python UUID is

ab2d0fc0-7224-11ec-8ef2-b658b885fb3

Also read,  Python filename extensions

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Generating Python UUID

Let’s learn more about Python UUID with the help of some examples.
 

Example 1: Using uuid1() for creating a Python uuid.

import uuid
print("1st Python UUID generated using uuid1: ", uuid.uuid1())
print("2nd Python UUID generated using uuid1: ", uuid.uuid1())

 

Output

1st Python UUID generated using uuid1:  dadc8e42-e516-11ed-8ff1-8713f965a59e
2nd Python UUID generated using uuid1:  dadc91bc-e516-11ed-8ff1-8713f965a59e

 

Explanation:

As you can see in the above example, in output, some parts of the Python uuid generated are the same as uuid1() generates a unique ID using date-time and the MAC address. The MAC address for the device is the same, so it reflects in the output.

 

Example 2: Generating Python uuid using uuid4().

import uuid
print("1st Python UUID generated using uuid4: ", uuid.uuid4())
print("2nd Python UUID generated using uuid4: ", uuid.uuid4())

 

Output

1st Python UUID generated using uuid4:  48558b3c-3263-4e93-be1e-2b64bf107649
2nd Python UUID generated using uuid4:  76427134-49c2-4c54-aa24-8c2ffadebe61

 

Explanation:

In this example, the two Python UUIDs generated do not have any component in common because the uuid4() generates random numbers. We prefer to use uuid4() instead of uuid1() in case of secure privacy.

 

Example 3: Use of uuid3() and uuid5() in Python

Both the identifiers, uuid3() and uuid5(), generate UUIDs by hashing namespace identifiers with the given name. The major difference between them is the hashing technique they implement. The uuid3() uses MD5 hashing technique; in contrast, the uuid5() uses SHA-1 as hashing technique.

 

Syntax:

uuid3(namespace_identifier, name/string)
uuid5(namespace_identifier, name/string)

 

Following are the different namespace identifiers used with UUID3 and UUID5,

  • NAMESPACE_DNS generates UUIDs representing the domain name,
     
  • NAMESPACE_URL generates UUIDs representing the URL,
     
  • NAMESPACE_OID is specified as the string ISO OIDand
     
  • NAMESPACE_X500.
     

Let’s generate some UUIDs using the above-mentioned identifiers.

For example,

import uuid
Page = "www.codingninjas.com"
print(uuid.uuid3(uuid.NAMESPACE_DNS, Page))
print(uuid.uuid3(uuid.NAMESPACE_URL, Page))
print(uuid.uuid3(uuid.NAMESPACE_OID, Page))
print(uuid.uuid3(uuid.NAMESPACE_X500, Page))

 

Output:

3488a56f-4399-39a9-a63d-5105d9f928f5
45ef213c-0751-35eb-b892-1803e266f63f
b52c87d8-ab3e-3cc0-b95e-7c318764369f
98ab6268-d965-3e07-a286-f1f5b6d2f234

 

Explanation:

Every time we use the above commands to get the ID for www.codingninjas.com using the different identifiers, it will produce the same as shown above, as a unique identifier is assigned to that particular domain.

Also see, How to Check Python Version in CMD

Advantages and Disadvantages of UUID

After discussing the structure and use case examples of Python UUID, let's see some advantages and disadvantages of Python UUID.

Advantages of UUID

Some of the advantages of Python UUIDs are as follows:

  1. The UUID depends on factors like date, time, MAC address, and name. This creates these IDs universally unique.
     
  2. The Python UUIDs are very easy to use.
     
  3. Python has a built-in module to generate UUIDs, so we don’t need to download any other packages. This built-in support is one of the significant advantages of Python uuid.
     
  4. There are different versions of UUIDs, from which users can select any according to their needs.

Disadvantages of UUID

Some of the disadvantages of Python UUIDs are as follows:

  1. Our MAC address is associated with the UUIDs we generate using uuid1(), so this can negatively impact in case of privacy and security issues.
     
  2. It takes up much more space than other identifiers.
     
  3. UUID is database dependent if generated using a database function.
     
  4. These IDs can only be understood with additional information, as these are not in human-readable form.
     
  5. Unlike other unique keys, the Python UUIDs are not generated in sequential order. These random generations create difficulties during the sorting of these IDs. 
     

Related Article MySQL UUID and Django Model data types and fields list.

Frequently Asked Questions

What are the different versions of UUIDs?

Different types of UUIDs are versions 1,2,4,3, and 5. Version 1 generated uuid using date-time and MAC address, Version 2 also generates using the same components, but it is kept as reserved. Version 4 randomly generates UUIDs. Version 3 and 5 generates using hashing identifier with names.

What is the use of UUIDs?

The generation of UUIDs can be used in web applications and database systems. In web applications, uuid helps in managing by creating unique session keys. In the case of a database, it is used as a unique database key.

What are the hashing algorithms used by UUID versions 3 and 5?

The UUID version 3 uses the MD5 hashing algorithm. The MD5 algorithm stands for the message-digest algorithm. Whereas version 5 uses the Secure Hash Algorithm, also known as the SHA-1 algorithm. They generate UUIDs by hashing a namespace identifier and name.

Conclusion

We hope this article was insightful and you learned something new. In this blog, we learned about UUIDs which we can use to identify data in computer systems. We discussed its different versions and learned their syntaxes with the help of some examples. It also has a few disadvantages, which we discussed at the end.
 

If you want to learn more about UUID, do visit 

Do visit our website to read more such blogs. Refer to our Guided Path on Coding Ninjas Studio to upskill yourself! You can check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! If you are looking for questions asked by tech giants, in that case, you must look at the problemsinterview experiences, and interview bundles for placement preparations.
 

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!!

Previous article
Popular Python Libraries
Next article
3D Curve Plots in matplotlib
Live masterclass