Appearance
How to create a GUID/UUID in Python
You can use the uuid module in Python to generate a globally unique identifier (GUID), also known as a universally unique identifier (UUID). Here is an example of how to generate a UUID in Python:
Generate a random uuid in Python
python
import uuid
# Generate a random UUID
random_uuid = uuid.uuid4()
# Print the UUID
print(random_uuid)
<div class="alert alert-info flex not-prose">Watch a video course Python - The Practical Guide
</div>
You can also use the uuid1() function to generate a UUID based on the host ID and current time:
Generate a uuid based on host ID and current time in Python
python
import uuid
# Generate a UUID based on the host ID and current time
time_based_uuid = uuid.uuid1()
# Print the UUID
print(time_based_uuid)You can also use uuid3() and uuid5() to create UUIDs based on the MD5 and SHA-1 hash respectively, with an additional namespace.
Generate a uuid based on the MD5 and SHA-1 hash respectively, with an additional namespace in Python
python
import uuid
# Generate a UUID based on the namespace and name
uuid3_uuid = uuid.uuid3(uuid.NAMESPACE_DNS, "example.com")
uuid5_uuid = uuid.uuid5(uuid.NAMESPACE_DNS, "example.com")
# Print the UUID
print(uuid3_uuid)
print(uuid5_uuid)