Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Create a URL slug from text

Convert arbitrary text into a clean, SEO-friendly URL slug by normalizing, removing non-alphanumerics, and joining words with hyphens.

Python practice17 minString PatternsIntermediateLast updated March 18, 2026

Problem statement

Implement slugify(text, max_words=5) that converts an input string into a lowercase URL slug. The slug should: - Normalize unicode characters to their closest ASCII representation (e.g., 'Café' -> 'cafe'). - Keep only ASCII letters and digits; remove punctuation and other symbols. - Split text into words based on alphanumeric runs, then join the first max_words words with single hyphens ('-'). - Collapse multiple separators and strip leading/trailing hyphens. If no words remain, return an empty string. The function must accept an optional max_words parameter to limit the number of words included in the slug (default 5).

Task

Learn Unicode normalization, token extraction, and safe slug construction used in web applications.

Examples

Basic slug

Input

Hello, World! This is Python.

Output

hello-world-this-is-python

Lowercased, punctuation removed, first 5 words joined with hyphens.

Input format

A string text and optional integer max_words passed to slugify(text, max_words).

Output format

A slug string using hyphens to join words, or an empty string if no words.

Constraints

Use Unicode normalization to map common accented characters to ASCII. Only ASCII letters and digits should appear in the final slug. max_words is a positive integer; if set to 0, return an empty string.

Samples

Sample 1

Input

This --- is crazy!!

Output

this-is-crazy

Multiple separators and punctuation are removed; whitespace collapsed and hyphens used to join words.