Convert snake_case to camelCase in Python

Convert a snake_case string to camelCase.

IntermediateString ProgramsExample 25 of 25
convert-snake-case-to-camelcase.py
Run in browser
1# Program to convert snake_case to camelCase
2
3snake = input("Enter snake_case string: ")
4
5parts = snake.split("_")
6
7if not parts:
8 camel = ""
9else:
10 camel = parts[0].lower() + "".join(word.capitalize() for word in parts[1:])
11
12print("camelCase:", camel)

Output

Enter snake_case string: hello_world_example
camelCase: helloWorldExample

What's going on

We split on underscores and capitalize each word after the first, then join them back.