Convert Strings to URL-Friendly Slugs

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In web development, it is common to have URLs that contain readable words instead of random characters. These readable words are called slugs. Slugs are used to make URLs more user-friendly and easier to remember. In this challenge, you will create a function that converts a string to a URL-friendly slug.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/AdvancedTopicsGroup(["`Advanced Topics`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/AdvancedTopicsGroup -.-> python/regular_expressions("`Regular Expressions`") subgraph Lab Skills python/comments -.-> lab-13715{{"`Convert Strings to URL-Friendly Slugs`"}} python/lists -.-> lab-13715{{"`Convert Strings to URL-Friendly Slugs`"}} python/tuples -.-> lab-13715{{"`Convert Strings to URL-Friendly Slugs`"}} python/function_definition -.-> lab-13715{{"`Convert Strings to URL-Friendly Slugs`"}} python/importing_modules -.-> lab-13715{{"`Convert Strings to URL-Friendly Slugs`"}} python/standard_libraries -.-> lab-13715{{"`Convert Strings to URL-Friendly Slugs`"}} python/regular_expressions -.-> lab-13715{{"`Convert Strings to URL-Friendly Slugs`"}} end

String to Slug

Write a function slugify(s) that takes a string s as an argument and returns a slug. The function should perform the following operations:

  1. Convert the string to lowercase and remove any leading or trailing whitespace.
  2. Replace all special characters (i.e., any character that is not a letter, digit, whitespace, hyphen, or underscore) with an empty string.
  3. Replace all whitespace, hyphens, and underscores with a single hyphen.
  4. Remove any leading or trailing hyphens.
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'

Summary

In this challenge, you learned how to create a function that converts a string to a URL-friendly slug. You used string methods and regular expressions to remove special characters and replace whitespace, hyphens, and underscores with a single hyphen. By completing this challenge, you have gained a better understanding of how to manipulate strings in Python.

Other Python Tutorials you may like