UUIDs are just 128 bit pieces of data, that is displayed as (128/4) = 32 hexadecimal digits, like this :

  • UUID v1 : c1b33c74-0006-11eb-9894-c83dd482e3ef
  • UUID v4 : 8791f25b-d4ca-4f10-8f60-407a507edefe

Python has built-in uuid library to generate UUID easily:

$ python3
Python 3.8.1 (default, Feb 12 2020, 16:30:11)
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import uuid
>>> print(uuid.uuid4())
8791f25b-d4ca-4f10-8f60-407a507edefe

OK, how can we turn 8791f25b-d4ca-4f10-8f60-407a507edefe to 8791f25bd4ca4f108f60407a507edefe ?

There are regex solutions for this problem but I like to use python to solve this.

# utils.py
from uuid import UUID

def uuid_to_hex(uuid):
    """Turn uuid4 with dashes to hex

    From : 8791f25b-d4ca-4f10-8f60-407a507edefe
    To   : 8791f25bd4ca4f108f60407a507edefe

    :param uuid: uuid string with dashes
    :type uuid: str

    :returns: str - hex of uuid
    """

    return UUID(uuid).hex

Example usage of this function:

$ python3
Python 3.8.1 (default, Feb 12 2020, 16:30:11)
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from utils import uuid_to_hex
>>> _uuid = "8791f25b-d4ca-4f10-8f60-407a507edefe"
>>> print(uuid_to_hex(_uuid))
8791f25bd4ca4f108f60407a507edefe

And yes, I like docs very much:

>>> uuid_to_hex.__doc__
'Turn uuid4 with dashes to hex\n\n    From : 8791f25b-d4ca-4f10-8f60-407a507edefe\n    To   : 8791f25bd4ca4f108f60407a507edefe\n\n    :param uuid: uuid string with dashes\n    :type uuid: str\n\n    :returns: str - hex of uuid\n    '

We can write this funtion also with Type Hints which introduced in PEP 484 in order to make it more readable(?). Type Hints requires Pythhon >= 3.5.

# utils.py
from uuid import UUID

def uuid_to_hex(uuid: str) -> str:
    """Turn uuid4 with dashes to hex

    From : 8791f25b-d4ca-4f10-8f60-407a507edefe
    To   : 8791f25bd4ca4f108f60407a507edefe

    :param uuid: uuid string with dashes
    :type uuid: str

    :returns: str - hex of uuid
    """

    return UUID(uuid).hex

All done!