Back to Practice
#0051

Slugify a Title

EasyPython12 min10 XP

Problem

Given a title string, return a lowercase slug. Keep letters and digits, convert runs of non-alphanumeric characters into a single hyphen, and remove leading or trailing hyphens.

Why This Matters

String cleaning appears in filenames, URLs, feature names, search indexes, and ETL pipelines. This problem checks whether you can normalize text without overcomplicating it.

Function Signature

def slugify(title):

Examples

Example 1
Inputtitle = " Python Lists & Dicts!! "
Output"python-lists-dicts"

Spaces, ampersand, and exclamation marks become separators, but repeated separators collapse into one hyphen.

Constraints

  • Input contains ordinary ASCII letters, digits, spaces, and punctuation.
  • Return an empty string if no alphanumeric character exists.
  • Do not use a regex-only one-liner; the goal is to understand the scan.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Messy punctuation
Input" Python Lists & Dicts!! "
Expected"python-lists-dicts"

The slug is lowercase and has one hyphen between words.

Digits stay
Input"ML 101: Week #2"
Expected"ml-101-week-2"

Digits are valid slug characters, punctuation is not.

Hidden Test Categories
Only punctuationAlready clean slugMultiple separators in a row