Slugify a Blog Title

Medium ⏱ 12 min 47% acceptance ★★★★☆ 4.4
Write slugify(title) converting any title to a URL slug: lowercase, non-alphanumeric runs become single hyphens, and leading/trailing hyphens are trimmed — two re.sub calls plus strip('-').

Examples

Example 1
Input
slugify('  10 Tips: Python & SQL!  ')
Output
'10-tips-python-sql'
Explanation

Symbols and spaces collapsed into hyphens.

Constraints

  • Only [a-z0-9-] in the output.
  • No doubled or edge hyphens.

Topics

Regular Expressionssub pipeline

Companies

WordPress VIPMediumSubstack

Hints

Hint 1

First sub: [^a-z0-9]+ → '-' on the lowercased title.

Hint 2

Then .strip('-').

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