Python UUID Module | Generate Universally Unique Identifiers (UUID)

UUIDs (Universally Unique Identifiers) are 128-bit unique identifiers that are used to identify information in computer systems. UUIDs are used in many applications, including databases, distributed systems, and messaging applications.

The Python UUID module provides various functions for generating UUIDs. Let’s take a look at each of these methods one by one using numerous examples:

  • Method 1: Using uuid1()
    • Generate UUID
    • Generate UUID in Various Formats
  • Method 2: Using uuid4() to Generate UUID
  • Method 3: Using uuid3()
    • Generate UUID
    • Generate UUID of Various Hostnames
    • Generate UUID of Various Namespace
  • Method 4: Using uuid5() to Generate UUID

Method 1: Using uuid1()

The “uuid1()” function of the uuid module is used to generate a UUID (Universally Unique Identifier) based on the current time and the MAC address of the computer. A UUID is a 128-bit number that identifies documents, hosts, or other entities within a system. 

Syntax: 

uuid.uuid1(node=None, clock_seq=None)

The uuid1() function returns a UUID object, which has various read-only attributes and can be converted to a string representation. Let’s understand the working of “uuid1()” using the following examples:

Example 1: Generate Universal Unique Identifiers

The below example is used to generate the Universal Unique Identifiers using the “uuid1()” function of “uuid” module:

Code:

import uuid 
print ("Random Generated uuid is : ", uuid.uuid1())

The uuid module is imported and the function “uuid.uuid1()” is used to generate the universally unique identifiers.

Output:

The random id has been generated using the uuid1() function.

Example 2: Generate UUID in Various Formats

The below code is used to generate the UUID in various formats such as bytes, int, hex, etc:

Code:

import uuid 
id = uuid.uuid1()  
# Representations of uuid1() 
print ("\nByte Representations: ", repr(id.bytes))
print ("\nInteger Representation: ",id.int)
print ("\nHex Representation: ",id.hex)

The ”uuid.uuid1()” is used to create the random id and stored in the variable “id”. The three different representations of the UUID object, such as the bytes attribute, int attribute, and the hex attribute of the UUID, are returned.

Output:

The bytes, int, and hex representations have been displayed.

Method 2: Using uuid4() to Generate Universal Unique Identifiers

The “uuid4()” function can also generate a random UUID. In contrast to the “uuid4()” function, which generates a random UUID, the “uuid1()” function generates a UUID based on the host ID, sequence number, and current time. Let’s understand its works using the below-given code:

Code:

import uuid 
print ("Random Generated uuid is : ", uuid.uuid4())

The “uuid4()” function of the uuid module is used to generate the random universally unique identifiers.

Output:

The random id has been generated.

Method 3: Using uuid3()

The “uuid3()” function is used to generate the uuid based on the MD5 hash of a namespace identifier and a string. 

A namespace identifier is a UUID representing a specific namespace, such as a URL or a domain name. A name is a string that identifies an item within that namespace. Let’s understand it by the following examples:

Example 1: Generate Universal Unique Identifiers

The following example code is used to generate the universally unique identifiers using the specific hostname and namespace parameter:

Code:

import uuid 
print ("Random Generated uuid is : ", uuid.uuid3(uuid.NAMESPACE_DNS, 'itslinuxfoss.com'))

The “uuid.uuid3()” function takes the “uuid.NAMESPACE_DNS” and “itslinuxfoss.com” hostname as an argument to return the unique random id. 

Output:

The random UUID has been created. 

Example 2: Generate Universal Unique Identifiers of Various Hostnames

The below example code is used to generate the unique identifiers of various hostnames:

Code:

import uuid 
hostn = ['itslinuxfoss.com', 'youtube.com'] 
for host in hostn: 
    print('UUID Generated: ',uuid.uuid3(uuid.NAMESPACE_DNS, host))
    print()

The list of various hostnames is created. The for loop iterates over the host names list and for each hostname the “uuid.uuid3()” function creates the random unique id.

Output:

The UUID of various hostnames with the same NAMESPACE is generated.

Example 3: Generate Universal Unique Identifiers of Various Namespaces

In the below example, the various namespaces are used to generate the universally unique identifiers:

Code:

import uuid 
name_space = [uuid.NAMESPACE_DNS, uuid.NAMESPACE_URL, uuid.NAMESPACE_OID, uuid.NAMESPACE_X500] 
for ns in name_space: 
    print('UUID Generated: ',uuid.uuid3(ns, 'itslinuxfoss.com'))
    print()

The different namespaces are placed as the element of the list. The for loop iterates over the list and for each list element the “uuid.uuid3()” function returns a unique id.

Output:

The UUID of various namespaces has been generated.

Method 4: Using uuid5() to Generate Universal Unique Identifiers

The “uuid5()” function generates a UUID based on the SHA-1 hash of a namespace identifier and a name. The namespace identifier and the name are used to ensure that the same UUID is generated for the same input. The below example code is used to generate the UUID using the uuid5() function:

Code:

import uuid 
print ("Random Generated uuid is : ", uuid.uuid5(uuid.NAMESPACE_DNS, 'itslinuxfoss.com'))

The “uuid5()” takes the namespace and hostname values as a partner and returns the random unique id.

Output:

The random UUID has been generated.

Conclusion

To generate the UUID (Universal Unique Identifiers) the “uuid1()”, “uuid4()”, “uuid3()” and “uuid5()” functions of the UUID module are used in Python. The “uuid1()” and “uuid4()” are used to generate a unique UUID. But the UUID generated by uuid1() is based on the host ID, sequence number, and current time while uuid4() generates a random UUID. The uuid5() and uuid3() can generate the UUID based on various namespaces and hostnames. 

All the above-stated methods are briefly described in this post.