Skip to content

Instantly share code, notes, and snippets.

@entirelymagic
Created December 7, 2021 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save entirelymagic/1611de2294255bd557aa7eb96300800e to your computer and use it in GitHub Desktop.
Save entirelymagic/1611de2294255bd557aa7eb96300800e to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment