September 2021

Generate A UUID with .uuid4 in Python

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