home ~ socials ~ projects ~ rss

Generate A UUID with .uuid4 in Python

September 2021

First off, standard UUIDs are 36 characters (32 hex characters plus four dashes)

From the docs: If all you want is a unique ID, you should probably call uuid1() or uuid4(). Note that uuid1() may compromise privacy since it creates a UUID containing the computer’s network address.

uuid4() creates a random UUID.

So,

import uuid

my_uuid = str(uuid.uuid4())

print(my_uuid)
Output:
4aa74f3e-c585-4103-881e-56a3e480d04e

Note that the value is a UUID object, but it converts to a string when printed. In this case, we're making the move explicitly.

end of line
Share link:
https://www.alanwsmith.com/en/20/eo/pm/hp/?generate-a-uuid-with-uuid4-in-python