Kebabcase string

OSCC · Python, String, Regexp · Nov 2, 2020

Converts a string to kebab case.

  • Use re.sub() to replace any - or _ with a space, using the regexp r"(_|-)+".
  • Use re.sub() to match all words in the string, str.lower() to lowercase them.
  • Finally, use str.join() to combine all word using - as the separator.
from re import sub

def kebab(s):
  return '-'.join(
    sub(r"(\s|_|-)+"," ",
    lambda mo: ' ' +, s)).split())
kebab('camelCase') # 'camel-case'
kebab('some text') # 'some-text'
kebab('some-mixed_string With spaces_underscores-and-hyphens')
# 'some-mixed-string-with-spaces-underscores-and-hyphens'
kebab('AllThe-small Things') # 'all-the-small-things'

Recommended snippets