Menu

gitpiper

slugify python Code Snippet in 2024

stringregexpintermediate

Last Updated: 21 April 2024

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 April 2024

Subscribe to our Newsletter

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

© 2024 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! ✌️