30 seconds of python

  Back to Home



Converts a string to camelcase.

Use re.sub() to replace any - or _ with a space, using the regexp r"(_|-)+". Use title() to capitalize the first letter of each word convert the rest to lowercase. Finally, use replace() to remove spaces between words.

import re

def camel(s):
  s = re.sub(r"(_|-)+", " ", s).title().replace(" ", "")
  return s[0].lower() + s[1:]