0
0
PythonProgramBeginner · 2 min read

Python Program to Convert String to Camel Case

Use ''.join(word.capitalize() if i != 0 else word.lower() for i, word in enumerate(input_string.split())) to convert a string to camel case in Python.
📋

Examples

Inputhello world
OutputhelloWorld
Inputconvert this string
OutputconvertThisString
Input multiple spaces here
OutputmultipleSpacesHere
🧠

How to Think About It

To convert a string to camel case, split the string into words by spaces, make the first word all lowercase, and capitalize the first letter of each following word. Then join all words together without spaces.
📐

Algorithm

1
Get the input string.
2
Split the string into words using spaces.
3
Make the first word lowercase.
4
Capitalize the first letter of each remaining word.
5
Join all words together without spaces.
6
Return the resulting camel case string.
💻

Code

python
def to_camel_case(s):
    words = s.split()
    if not words:
        return ''
    return words[0].lower() + ''.join(word.capitalize() for word in words[1:])

# Example usage
input_string = "hello world"
print(to_camel_case(input_string))
Output
helloWorld
🔍

Dry Run

Let's trace 'hello world' through the code

1

Split string

Input string 'hello world' is split into ['hello', 'world']

2

Process first word

First word 'hello' is converted to lowercase 'hello'

3

Process remaining words

Second word 'world' is capitalized to 'World'

4

Join words

Join 'hello' + 'World' to get 'helloWorld'

StepWords ListResult String
After split['hello', 'world']
After first word lowercase['hello', 'world']hello
After capitalizing rest['hello', 'world']helloWorld
💡

Why This Works

Step 1: Splitting the string

We split the input string into words using split() so we can handle each word separately.

Step 2: Lowercase first word

The first word is made lowercase to follow camel case rules where the first word starts with a lowercase letter.

Step 3: Capitalize subsequent words

Each word after the first is capitalized to mark the start of a new word in camel case.

Step 4: Joining words

All words are joined without spaces to form the final camel case string.

🔄

Alternative Approaches

Using regular expressions
python
import re

def to_camel_case_regex(s):
    words = re.split(r'\s+', s.strip())
    if not words:
        return ''
    return words[0].lower() + ''.join(word.capitalize() for word in words[1:])

print(to_camel_case_regex('convert this string'))
Handles multiple spaces and trims input but requires importing regex module.
Using title() and manual fix
python
def to_camel_case_title(s):
    s = s.title().replace(' ', '')
    return s[0].lower() + s[1:] if s else ''

print(to_camel_case_title('hello world'))
Simpler but may incorrectly capitalize letters after apostrophes or special characters.

Complexity: O(n) time, O(n) space

Time Complexity

The program processes each character once during splitting and joining, so it runs in linear time relative to the input string length.

Space Complexity

Extra space is used to store the list of words and the output string, both proportional to the input size.

Which Approach is Fastest?

The basic split and join method is fastest and simplest; regex adds overhead but handles edge cases better.

ApproachTimeSpaceBest For
Split and joinO(n)O(n)Simple and common cases
Regex splitO(n)O(n)Handling multiple spaces and trimming
title() methodO(n)O(n)Quick conversion but less precise
💡
Always strip and split the string to handle extra spaces before converting to camel case.
⚠️
Forgetting to lowercase the first word, which breaks the camel case convention.