On this page
article
Slugify String
Convert title to URL slug.
Category: strings
Problem
Convert title to URL slug.
Solution
slug = re.sub(r'[^a-z0-9]+', '-', title.lower()).strip('-')
Notes
- Adapt variable names and paths to your project
- Add error handling for production use
- See related chapters in the Learning Path