Menu

gitpiper

slugify python Code Snippet in 2023

stringregexpintermediate

Last Updated: 4 February 2023

Converts a string to a URL-friendly slug.

  • Use str.lower() and str.strip() to normalize the input string.
  • Use re.sub() to to replace spaces, dashes and underscores with - and remove special characters.
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'

python snippet similar to slugify For You in February 2023

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2023 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️