Search code examples
pythonuuid

Python convert str variable to UUID type


I am trying to convert str variable to UUID type. Online tutorials point to below code

import uuid
delete_uuid = "5d27bf88-f3dd-4e95-89c1-f200c8484b42"
your_uuid_string = uuid.UUID(delete_uuid).hex

print(type(your_uuid_string))

but the output is still of type str. Please guide


Solution

  • If you need to get a UUID object as output you need to remove .hex, as UUID.hex returns a str:

    The UUID as a 32-character hexadecimal string.

    So, you can use

    >>> import uuid
    >>> delete_uuid = "5d27bf88-f3dd-4e95-89c1-f200c8484b42"
    >>> your_uuid_string = uuid.UUID(delete_uuid)
    >>> type(your_uuid_string)
    <class 'uuid.UUID'>