Creates a clean, readable URL slug from any given text. Slugs are commonly used in blogs, documentation tools, and web apps.
Example:
- Input: "Python Utilities for Text Processing"
- Output: "python-utilities-for-text-processing"
import re
def generate_slug(text):
# Lowercase
text = text.lower()
# Replace non‑alphanumeric characters with spaces
text = re.sub(r"[^a-z0-9]+", " ", text)
# Remove extra spaces
text = text.strip()
# Replace spaces with hyphens
return text.replace(" ", "-")
print(generate_slug("Python Utilities for Text Processing"))
# Output: python-utilities-for-text-processingThis utility performs:
- Lowercasing for consistency.
- Removal of symbols and punctuation.
- Normalization of spacing.
- Conversion of spaces to hyphens.
The result is a clean, SEO‑friendly slug.
title = "Learn Python in 2025: Complete Roadmap!"
print(generate_slug(title))
# Output: learn-python-in-2025-complete-roadmapUseful for generating blog URLs, file names, or identifiers.
- Simple text‑to‑slug utility.
- Ensures lowercase, readable, hyphenated output.
- Ideal for websites, blogs, and documentation.
Utility 29: Convert snake_case to camelCase.