Search code examples
pythonpython-3.xcasecamelcasing

Non camel case to Camel Case in python 3?


This program is meant to change something from normal to camelCase. EG: Not_Camel_case -> notCamelCase or Camel_Case to camelCase.

def titlecase(value):
    return "".join(word.title() for word in value.split("_"))

def titlecase2(value):
    return value[:1].lower() + titlecase(value)[1:]
def to_camel(value):
    return titlecase2(value)

This outputs what i want BUT..... This is for a competition and putting in Not_An_SMS returns notAnSms instead of notAnSMS? Also putting in num2words is supposed to return the same but instead my program capitalizes it like num2Words. What do i do to fix these problems?

EDIT: I have to change things within the functions not the output as the comp directly checks the functions in particular to_camel.


Solution

  • If you want Not_An_SMS to return notAnSms, you have to stop using the word.title() function, and instead just uppercase/lowercase the first letter of each word, but preserve the case of the others.

    That will still mean that SMS_status will return sMSStatus but I guess that can be considered an exception. If you want to fix that you'll need to have a dictionary of words so you can figure out if something is a word or not. And that's certainly not withing the scope of the competition.