Convert String to Kebab Case in Python (Challenge)

PythonPythonBeginner
Practice Now

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

Introduction

In Python, a kebab case string is a string where each word is separated by a hyphen. For example, "hello-world" is a kebab case string. In this challenge, you will be tasked with writing a function that converts a given string to kebab case.


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/BasicConceptsGroup -.-> python/comments("`Comments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/sets("`Sets`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/lambda_functions("`Lambda Functions`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") subgraph Lab Skills python/comments -.-> lab-13143{{"`Convert String to Kebab Case in Python (Challenge)`"}} python/with_statement -.-> lab-13143{{"`Convert String to Kebab Case in Python (Challenge)`"}} python/lists -.-> lab-13143{{"`Convert String to Kebab Case in Python (Challenge)`"}} python/tuples -.-> lab-13143{{"`Convert String to Kebab Case in Python (Challenge)`"}} python/sets -.-> lab-13143{{"`Convert String to Kebab Case in Python (Challenge)`"}} python/function_definition -.-> lab-13143{{"`Convert String to Kebab Case in Python (Challenge)`"}} python/lambda_functions -.-> lab-13143{{"`Convert String to Kebab Case in Python (Challenge)`"}} python/importing_modules -.-> lab-13143{{"`Convert String to Kebab Case in Python (Challenge)`"}} python/using_packages -.-> lab-13143{{"`Convert String to Kebab Case in Python (Challenge)`"}} python/standard_libraries -.-> lab-13143{{"`Convert String to Kebab Case in Python (Challenge)`"}} end

Kebabcase string

Problem

Write a Python function called to_kebab_case(s) that takes a string s as its input and returns the kebab case version of the string. The function should perform the following steps:

  1. Replace any - or _ with a space, using the regexp r"(_|-)+".
  2. Match all words in the string, str.lower() to lowercase them.
  3. Combine all words using - as the separator.

Example

to_kebab_case('camelCase') ## 'camel-case'
to_kebab_case('some text') ## 'some-text'
to_kebab_case('some-mixed_string With spaces_underscores-and-hyphens')
## 'some-mixed-string-with-spaces-underscores-and-hyphens'
to_kebab_case('AllThe-small Things') ## 'all-the-small-things'

Summary

In this challenge, you learned how to convert a string to kebab case in Python. You used re.sub() to replace any - or _ with a space, re.sub() to match all words in the string, str.lower() to lowercase them, and str.join() to combine all words using - as the separator.

Other Python Tutorials you may like