UUID (Universally Unique Identifier) is a 128-bit unique identifier that is used to uniquely identify information in systems and software applications. They are especially applied widely in cryptography and hashing algorithms as well as generally where where unique ids are needed.

UUIDs are normally represented as a string of 32 hexadecimal digits separated by hyphens, such as "2a3f1c3e-7d44-4e27-8b1c-9915b801d1de".

The uuid module in the standard library provide tools for generating UUIDs.

uuid.uuid1()

uuid1 (uuid version 1) generates UUIDs based on MAC/hardware address of the computer. To view the MAC address of the computer, we can use the uuid.getnode() function as shown below:

ExampleEdit & Run
import uuid

print(uuid.getnode())
copy
Output:
262175419772115 [Finished in 0.019662602804601192s]

The uuid1() function uses the MAC address with a combination of letters and current time to generate the UUID. Consider the following example:

ExampleEdit & Run
import uuid

u = uuid.uuid1()

print(u)
print("version: ", u.version)
copy
Output:
4e2a582a-bef5-11ef-8837-ee7278c3acd3 version:  1 [Finished in 0.01890138117596507s]

The MAC Adress  is included as the last part of the UUID in its hexadecimal form. The function allows an argument called node which if specified, will be used instead of the MAC address.

ExampleEdit & Run

with a custom node value

import uuid

print(uuid.uuid1(node = 0))
copy
Output:
4e2d63b1-bef5-11ef-8ce6-000000000000 [Finished in 0.020210156217217445s]

As you can see above, the last part of the UUID is just zeros, because we specified 0 as the node value.

UUID objects contains some useful attribute. For example, we can retrieve the MAC address, the time that was used to create th UUID and other useful attributes. All the attributes are as listed below:

Fields of uuid1()

  • time_low : The first 32 bits of id.
  • time_mid : next 16 bits of id.
  • time_hi_version : next 16 bits of id.
  • clock_seq_hi_variant : next 8 bits of id.
  • clock_seq_low : next 8 bits of id.
  • node : Last 48 bits of id.
  • time : time used for creation of the uuid.
  • clock_seq : 14 bit sequence number.
ExampleEdit & Run

UUID attributes

import uuid

u = uuid.uuid1()

#uuid fragments
print(u.time_low)
print(u.time_mid)
print(u.time_hi_version)
print(u.clock_seq_hi_variant)
print(u.clock_seq_low)
print(u.node) #last bit
copy
Output:
1311808360 48885 4591 152 229 262175419772115 [Finished in 0.018631682731211185s]

Apart from the above attributes, UUIDs stores other attributes for the representation of the uuid in other formats such as integer, bytes and hex.

ExampleEdit & Run
import uuid

u = uuid.uuid1()

print('hex: ', u.hex)
print('int: ', u.int)
print('bytes: ', u.bytes)
copy
Output:
hex:  4e33964cbef511ef85f1ee7278c3acd3 int:  103947639252888036520720202469604568275 bytes:  b'N3\x96L\xbe\xf5\x11\xef\x85\xf1\xeerx\xc3\xac\xd3' [Finished in 0.018683937843888998s]

To turn the UUID to string, we simply need to use the builtin str() function with the uuid as the argument.

ExampleEdit & Run
import uuid 

u = uuid.uuid1()

print(str(u))
copy
Output:
4e36973e-bef5-11ef-8eaf-ee7278c3acd3 [Finished in 0.018620137125253677s]

uuid.uuid4()

As we have seen from above examples, uuid1 is not entirely random as it uses well defined values to create the UUIDS. On the other hand, uuid4() is entirely random. This is the method that should be used when we need random UUIDs that are not based on any pre-defined format. 

ExampleEdit & Run
import uuid

u = uuid.uuid4()
print(u) 
copy
Output:
69945fcd-cbad-404d-bd05-463fb878e6c0 [Finished in 0.01907611219212413s]

Similarly, we can access attributes from the returned UUID as shown below.

ExampleEdit & Run
import uuid

u = uuid.uuid4()

print('Version: ', u.version)
print('hex: ', u.hex)
print('int: ', u.int)
print('bytes: ', u.bytes)
copy
Output:
Version:  4 hex:  9558b41ee6574b0389d7c170c934321b int:  198515546777364145670559280153056522779 bytes:  b'\x95X\xb4\x1e\xe6WK\x03\x89\xd7\xc1p\xc942\x1b' [Finished in 0.0187103901989758s]

To turn the UUID to string, simply use the builtin str() function. 

ExampleEdit & Run
import uuid

u = uuid.uuid4()
print(u)
print(str(u)) 
copy
Output:
94755a9e-5611-4b18-bb0b-1ca10c1aa833 94755a9e-5611-4b18-bb0b-1ca10c1aa833 [Finished in 0.01848324527963996s]