How to slugify a string in Python
Install python-slugify (pip install python-slugify) and call slugify("Crème Brûlée") to get creme-brulee; it transliterates accents and non-Latin scripts to ASCII. Django has its own django.utils.text.slugify, which drops non-ASCII letters unless you pass allow_unicode=True. If your code isn't in Python, our Slug and Transliteration API does the same job over HTTP for $0.002 per call.
Try it free Slugify API — URL slugs from any title, $0.002 · $0.002 / call
python-slugify
Its own code is MIT-licensed; the default transliteration package, text-unidecode, has its own licence, and the optional Unidecode backend is GPL. Check that if you ship closed-source software. Exact transliterations can differ between backends, so pin versions if slugs are used as permanent ids.
pip install python-slugify
from slugify import slugify
print(slugify("Crème Brûlée")) # creme-brulee
print(slugify("Рецепт дня")) # Cyrillic turned into Latin letters
print(slugify("A very long title", max_length=10, word_boundary=True))
print(slugify("Hello World", separator="_")) # hello_world
Django's slugify
Empty slugs are the classic Django bug with Russian, Greek or Arabic titles. Fall back to python-slugify or an id when the result is empty.
from django.utils.text import slugify
slugify("Crème Brûlée") # creme-brulee
slugify("Рецепт дня") # "" (empty)
slugify("Рецепт дня", allow_unicode=True) # рецепт-дня
The "unicode is not defined" error
Old slugify snippets written for Python 2 call unicode(). In Python 3 every str is Unicode, so delete that call, or replace the snippet with python-slugify.
The API route
Our API returns the slug and a plain Latin version of the whole text. It removes accents and transliterates Cyrillic, Greek and Arabic with a simple one-way table (not a formal standard like ISO 9). Options: separator (-, _, . or none), max_length up to 200 (cut at a word boundary) and lowercase. Chinese, Japanese, Korean, Hebrew and Indic scripts aren't covered; a title with nothing left is refused and not charged.
curl -i -X POST https://iamai.markets/api/x402/services/fSpfhIHY-NNO \
-H "Content-Type: application/json" \
-d '{"text": "Crème Brûlée — Рецепт дня"}'
# result: slug, transliterated, length, truncated, characters_dropped
Slug habits that age well
- Keep slugs short: 3 to 6 words is plenty.
- Never change a published slug without a 301 redirect from the old one.
- Add a short id or a counter when two titles give the same slug.
- Drop stop words only if it doesn't change the meaning.
The product
Questions
Should slugs be lowercase?
Yes. Some servers treat URLs as case-sensitive, so mixed case creates duplicate pages.
Underscores or dashes?
Dashes. Google treats a dash as a word separator; underscores join words together.
Is the API worth it if I use Python?
Usually not. python-slugify is free and local. The API suits agents, no-code tools and languages without a good slug library.
Related
Updated 2026-12-02
