Skip to content

Home

String to slug

Converts a string to a URL-friendly slug.

import re

def slugify(s):
  s = s.lower().strip()
  s = re.sub(r'[^\w\s-]', '', s)
  s = re.sub(r'[\s_-]+', '-', s)
  s = re.sub(r'^-+|-+$', '', s)
  return s

slugify('Hello World!') # 'hello-world'

More like this

Start typing a keyphrase to see matching snippets.