Python Slugify
Python-slugify is an actively maintained library that generates URL-friendly slugs from strings, with robust Unicode support. It is designed to create clean, human-readable, and SEO-friendly slugs by converting text, handling special characters, and replacing spaces. The library sees regular updates, with an average release cycle of approximately 71 days.
Warnings
- gotcha There is a separate, older Python 2-only package also named `slugify` on PyPI. Installing `pip install slugify` will give you the wrong package. Always use `pip install python-slugify` to ensure you get the correct, actively maintained Python 3 library, as both packages use `from slugify import slugify` for import.
- breaking Python 3.6 support was dropped in version 7.0.0. For the latest versions (8.x.x), the minimum required Python version is 3.10. Older Python versions should use earlier library releases.
- gotcha The optional `Unidecode` dependency (installed via `pip install python-slugify[unidecode]`) is GPL licensed. If your project's license is incompatible with GPL, ensure you do not install this extra. The default `text-unidecode` dependency is permissively licensed.
- breaking The behavior of the `regex_pattern` parameter was inverted in version 6.0.1. It now defines characters to be *disallowed* rather than *allowed*. This changes how custom character filtering works.
Install
-
pip install python-slugify
Imports
- slugify
from slugify import slugify
Quickstart
from slugify import slugify
# Basic usage
text = "This is a Test String with Unicode: Lörem Ipsüm!"
slug = slugify(text)
print(f"Basic Slug: {slug}")
# Custom separator
slug_with_underscore = slugify(text, separator='_')
print(f"Slug with underscore: {slug_with_underscore}")
# Max length
short_slug = slugify("A very very long title indeed", max_length=10)
print(f"Short Slug: {short_slug}")
# Keep unicode characters (requires allow_unicode=True)
unicode_slug = slugify("你好世界", allow_unicode=True)
print(f"Unicode Slug: {unicode_slug}")