How do I convert a Python UUID into a string?

How do I convert a Python UUID into a string?

you can try this !

 a = uuid.uuid1()
 str(a)
 --> 448096f0-12b4-11e6-88f1-180373e5e84a

You can also do this. Removes the dashes as a bonus. link to docs.

import uuid
my_id = uuid.uuid4().hex

ffba27447d8e4285b7bdb4a6ec76db5c

UPDATE: trimmed UUIDs (without the dashes) are functionally identical to full UUIDS (discussion). The dashes in full UUIDs are always in the same position (article).

How do I convert a Python UUID into a string?

I came up with a different solution that worked for me as expected with Python 3.7.

import uuid

uid_str = uuid.uuid4().urn
your_id = uid_str[9:]

urn is the UUID as a URN as specified in RFC 4122.

Leave a Reply

Your email address will not be published. Required fields are marked *