Menu

gitpiper

kebab python Code Snippet in 2024

stringregexpintermediate

Last Updated: 24 March 2024

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|_|-)+"," ", sub(r"[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+", lambda mo: ' ' + mo.group(0).lower(), 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'

python snippet similar to kebab For You in March 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️