camelCase in Python

CamelCase is a naming convention where the first word starts with a lowercase letter and each subsequent word starts with an uppercase letter, with no spaces between words. In Python, we can convert a list of words to camelCase format using string manipulation.

So, if the input is like ["Hello", "World", "Python", "Programming"], then the output will be "helloWorldPythonProgramming".

Algorithm Steps

To solve this, we will follow these steps ?

  • Initialize an empty string

  • For each word in the list ?

    • Make the first letter uppercase and rest lowercase

    • Concatenate the word with our string

  • Convert the first letter of the final string to lowercase

  • Return the result

Using List Comprehension and Join

Here's an efficient implementation using list comprehension ?

class Solution:
    def solve(self, words):
        s = "".join(word[0].upper() + word[1:].lower() for word in words)
        return s[0].lower() + s[1:]

ob = Solution()
words = ["Hello", "World", "Python", "Programming"]
print(ob.solve(words))
helloWorldPythonProgramming

Using a Simple Loop

An alternative approach using a traditional loop ?

def to_camel_case(words):
    result = ""
    for i, word in enumerate(words):
        if i == 0:
            # First word: all lowercase
            result += word.lower()
        else:
            # Subsequent words: capitalize first letter
            result += word.capitalize()
    return result

words = ["Hello", "World", "Python", "Programming"]
print(to_camel_case(words))
helloWorldPythonProgramming

Using Built-in String Methods

A more concise approach using built-in methods ?

def camel_case_converter(words):
    if not words:
        return ""
    
    # First word lowercase, rest capitalized
    camel_case = words[0].lower() + "".join(word.capitalize() for word in words[1:])
    return camel_case

words = ["Hello", "World", "Python", "Programming"]
result = camel_case_converter(words)
print(f"Input: {words}")
print(f"Output: {result}")
Input: ['Hello', 'World', 'Python', 'Programming']
Output: helloWorldPythonProgramming

Comparison

Method Pros Best For
List Comprehension Concise, Pythonic Short, readable code
Simple Loop Clear logic flow Beginners, debugging
Built-in Methods Handles edge cases Production code

Conclusion

Converting words to camelCase involves making the first word lowercase and capitalizing subsequent words. The list comprehension approach is most Pythonic, while the loop method offers better readability for beginners.

Updated on: 2026-03-25T10:16:18+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements