Search code examples
pythonuuid

How to insert hyphens into a UUID string?


I want to create a function that adds a - on position 8, 12, 16, 20 of a UUID string.

Example:

Original: 76684739331d422cb93e5057c112b637
New: 76684739-331d-422c-b93e-5057c112b637

I have no clue on how to position it on multiple positions in a string that wouldn't look like spaghetti code.


Solution

  • It looks like you are working with UUIDs. There's a library for that that comes standard with Python:

    import uuid
    s = '76684739331d422cb93e5057c112b637'
    u = uuid.UUID(hex=s)
    print(u)
    
    76684739-331d-422c-b93e-5057c112b637