Get uuid from a link string in python

Learn how to get UUID from a string link on a python programming language. This could be useful for example when you are working with Notion site then need to grab the id or any other need.

First we need to import uuid with uuid.UUID(string) will convert “anythathasdash” to “any-that-has-dash”

return str(uuid.UUID(string_id))

But let’s assume our string is not clean. It has several possibilites, like having string before or having question mark that normally shows up on a link, ect. Here’s how I solve it

import uuid

def __get_UUID_from_link(link):
    string_id = link.split("?")[0]
    string_id = string_id.split("-")[-1]
    string_id = string_id.split("/")[-1]
    return str(uuid.UUID(string_id))

# Several test cases
link1 =''
link2 = ''
link3 = ''
link4 = ''


Hope it helps you to solve how to convert a link string to a uuid format in Python

Online tool