import re
import unicodedata
def slugify(text: str, sep: str = "-") -> str:
text = unicodedata.normalize("NFKD", text)
text = "".join(c for c in text if not unicodedata.combining(c)) # strip accents
text = text.lower()
text = re.sub(r"[^a-z0-9]+", sep, text)
text = re.sub(rf"{re.escape(sep)}{{2,}}", sep, text)
return text.strip(sep)
slugify("Hello, World!") # 'hello-world'
slugify("Café au lait — délicieux") # 'cafe-au-lait-delicieux'
slugify("Python 3.12 & Beyond") # 'python-3-12-beyond'
Create a free account and build your private vault. Share publicly whenever you want.