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?)
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.
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.
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.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With