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