Python

Slugify (unicodedata, zero deps)

admin by @admin ADMIN
5d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Convert any string to a URL-safe slug using only stdlib. Normalizes Unicode (NFKD), strips combining marks, lowercases, replaces non-alphanumerics with a separator.
Python
Raw
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'
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.