Skip to content

Instantly share code, notes, and snippets.

@hsleonis
Created May 25, 2020 21:49
Show Gist options
  • Save hsleonis/a39dfc4bcbad4c19947cf402af00fe93 to your computer and use it in GitHub Desktop.
Save hsleonis/a39dfc4bcbad4c19947cf402af00fe93 to your computer and use it in GitHub Desktop.
Converts a string to camelcase.
from re import sub

def str_camelcase(s):
  s = sub(r"(_|-)+", " ", s).title().replace(" ", "")
  return s[0].lower() + s[1:]
str_camelcase('some_database_field_name') # 'someDatabaseFieldName'
str_camelcase('Some label that needs to be camelized') # 'someLabelThatNeedsToBeCamelized'
str_camelcase('some-javascript-property') # 'someJavascriptProperty'
str_camelcase('some-mixed_string with spaces_underscores-and-hyphens') # 'someMixedStringWithSpacesUnderscoresAndHyphens'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment