DEV Community

loading...

How to Remove Dashes in UUID

serhatteker profile image Serhat Teker Originally published at tech.serhatteker.com on ・2 min read

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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    '
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

All done!

Discussion (2)

pic
Editor guide
Collapse
otumianempire profile image
Otu Michael

Hello, I hope it is okay to drop the snippet below. I saw that you were using a hex, from UUID. Basically, split at the dashes and join with empty string.

from uuid import uuid4


def remove_dashes(uid):
    return "".join(str(uid).split("-"))


sample_uid = uuid4()

sample_output = remove_dashes(sample_uid)

print(str(sample_uid), sample_output, sep="\n")
# 52af7d72-3b7c-4048-b3b9-28dff0c8308d
# 52af7d723b7c4048b3b928dff0c8308d
Enter fullscreen mode Exit fullscreen mode
Collapse
serhatteker profile image
Serhat Teker Author

Seems cool; vanilla python -w/o any lib.