str.lower() and str.strip() to normalize the input string.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'
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️