Skip to main content

Generate URL Slugs in Python: slugify Complete Guide

Learn how to generate SEO-friendly URL slugs in Python using python-slugify and the standard library. Includes practical code examples.

Creating URL-safe slugs is a routine task in Python web development, whether you are building with Django, Flask, or a static site generator. A slug transforms an arbitrary string — like a blog post title — into a lowercase, hyphen-separated identifier safe for use in URLs.

How to Use the Slug Generator Tool

  1. Open the Slug Generator on ToolSparkr.
  2. Enter the text you want to convert — a title, heading, or label.
  3. The tool produces a clean slug in real time, stripping punctuation and replacing spaces with hyphens.
  4. Copy the output with one click and use it in your Python application or database.

Generating Slugs in Python with python-slugify

The python-slugify library is the standard choice. It handles Unicode, transliteration, and stop-word removal out of the box.

# pip install python-slugify
from slugify import slugify

title = \"Hello, World! This is a Python Slug Example\"
slug = slugify(title)
print(slug)  # hello-world-this-is-a-python-slug-example

# Custom separator and max length
slug_custom = slugify(title, separator='_', max_length=30)
print(slug_custom)  # hello_world_this_is_a_python

Pure Python Slug Function (No Dependencies)

If you cannot install third-party packages, a standard library approach using unicodedata and re covers most cases:

import re
import unicodedata

def slugify(text):
    text = unicodedata.normalize('NFD', text)
    text = text.encode('ascii', 'ignore').decode('ascii')
    text = text.lower()
    text = re.sub(r'[^a-z0-9\\s-]', '', text)
    text = re.sub(r'[\\s-]+', '-', text).strip('-')
    return text

print(slugify(\"Héllo Wörld — Python Test!\"))
# Output: hello-world-python-test

Why Use a Slug Generator?

  • URL safety — Encoded characters like %20 are ugly and error-prone; slugs keep URLs clean.
  • Database consistency — Storing slugs as the primary URL key prevents duplicate or ambiguous routes.
  • SEO — Keywords in the URL path are a confirmed ranking signal for search engines.
  • Framework compatibility — Django's slugify() and Flask-Slugify both follow the same conventions this tool produces.

Tips and Best Practices

  • Run unicodedata.normalize('NFD', ...) before encoding to ASCII so accented characters are transliterated rather than dropped.
  • Enforce a max_length constraint in your database column (typically 255 chars) and in your slug function.
  • Use a UNIQUE database constraint on your slug column and append a numeric suffix on collision (e.g., my-post-2).
  • In Django, the built-in django.utils.text.slugify() is sufficient for English content; use python-slugify for multilingual apps.

Need a quick conversion? Use the free online Slug Generator — paste your text and get an instant result.

Try Slug Generator Free

Generate URL-friendly slugs from text.

Use Slug Generator →