Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Kabob-Case A String In Python

This is an adaptation of my snake_case function (TKTKTK: link to snake_case function when the site is moved). Haven't put a full test suite on it yet, but spot checking it seems to be fine.

Code

#!/usr/bin/env python3

import re 

def kabob(initial_string):
    
    return_string = initial_string.lower().replace(' ', '-')
    return_string = re.sub("'", '', return_string)
    return_string = re.sub('[^\w\.]', '-', return_string)
    return_string = re.sub('-+', '-', return_string)
    return_string = re.sub('-\.', '.', return_string)
    return_string = re.sub('\.-', '.', return_string)
    return_string = re.sub('^-+', '', return_string)
    
    return return_string

kabob_string = kabob('Kabob Case A String In Python')

print(kabob_string)