Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Snake Case to Lower Camel Case (lowerCamelCase)

What would be a good way to convert from snake case (my_string) to lower camel case (myString) in Python 2.7?

The obvious solution is to split by underscore, capitalize each word except the first one and join back together.

However, I'm curious as to other, more idiomatic solutions or a way to use RegExp to achieve this (with some case modifier?)

like image 968
luca Avatar asked Sep 27 '13 14:09

luca


People also ask

Is snake case or camel case better?

Screaming snake case is used for variables. Scripting languages, as demonstrated in the Python style guide, recommend snake case in the instances where C-based languages use camel case.

How do you convert Snakecase to CamelCase in Python?

What would be a good way to convert from snake case ( my_string ) to lower camel case (myString) in Python 2.7? The obvious solution is to split by underscore, capitalize each word except the first one and join back together.

How do you write in lower camel case?

lowerCamelCase (part of CamelCase) is a naming convention in which a name is formed of multiple words that are joined together as a single word with the first letter of each of the multiple words (except the first one) capitalized within the new word that forms the name.


1 Answers

def to_camel_case(snake_str):     components = snake_str.split('_')     # We capitalize the first letter of each component except the first one     # with the 'title' method and join them together.     return components[0] + ''.join(x.title() for x in components[1:]) 

Example:

In [11]: to_camel_case('snake_case') Out[11]: 'snakeCase' 
like image 137
jbaiter Avatar answered Oct 20 '22 15:10

jbaiter