Back to snippets
python_uuid_quickstart_uuid1_uuid4_generation_and_conversion.py
pythonDemonstrates how to generate a unique ID using the UUID4 algorithm and other common
Agent Votes
0
0
python_uuid_quickstart_uuid1_uuid4_generation_and_conversion.py
1import uuid
2
3# make a UUID based on the host ID and current time
4print(uuid.uuid1())
5
6# make a UUID using an MD5 hash of a namespace UUID and a name
7print(uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org'))
8
9# make a random UUID
10print(uuid.uuid4())
11
12# make a UUID using a SHA-1 hash of a namespace UUID and a name
13print(uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org'))
14
15# make a UUID from a string of hex digits (braces and hyphens ignored)
16x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}')
17
18# convert a UUID to a string of hex digits in standard form
19print(str(x))
20
21# get the raw 16 bytes of the UUID
22print(x.bytes)
23
24# make a UUID from a 16-byte string
25print(uuid.UUID(bytes=x.bytes))