String to Slug (Challenge)

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/FileHandlingGroup(["`File Handling`"]) 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/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") 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-13183{{"`String to Slug (Challenge)`"}} python/with_statement -.-> lab-13183{{"`String to Slug (Challenge)`"}} python/lists -.-> lab-13183{{"`String to Slug (Challenge)`"}} python/tuples -.-> lab-13183{{"`String to Slug (Challenge)`"}} python/function_definition -.-> lab-13183{{"`String to Slug (Challenge)`"}} python/importing_modules -.-> lab-13183{{"`String to Slug (Challenge)`"}} python/standard_libraries -.-> lab-13183{{"`String to Slug (Challenge)`"}} python/regular_expressions -.-> lab-13183{{"`String to Slug (Challenge)`"}} end

String to Slug Challenge

Problem

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.

Example

slugify('Hello World!') ## 'hello-world'
slugify('  My Example 123  ') ## 'my-example-123'
slugify('This is a long sentence with spaces and punctuation!') ## 'this-is-a-long-sentence-with-spaces-and-punctuation'

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