Generate a URL Slug from a Title

Medium ⏱ 12 min 54% acceptance ★★★★☆ 4.3
Write generate_slug(title) that turns an article title into a clean, URL-friendly slug: lowercase, spaces/underscores turned into hyphens, all other punctuation dropped, and no repeated or leading/trailing hyphens.

Examples

Example 1
Input
title = '  Top 10 Python Tips & Tricks!!  '
Output
'top-10-python-tips-tricks'
Explanation

Whitespace trimmed, letters lowercased, spaces become hyphens, "&" and "!" are dropped, and the resulting double hyphen collapses to one.

Constraints

  • 1 <= len(title) <= 200

Topics

StringsBusiness Logic

Companies

NetflixLinkedInAtlassian

Hints

Hint 1

Strip and lowercase first.

Hint 2

Keep alphanumeric characters as-is; map spaces/underscores/hyphens to a single hyphen; drop everything else.

Hint 3

Collapse '--' to '-' in a loop, then strip leading/trailing hyphens.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.