Open In App

Python – Convert Snake Case String to Camel Case

Last Updated : 14 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Converting a string from snake case to camel case involves changing the format from words separated by underscores to a single string where the first word is lowercase and subsequent words start with uppercase letters.

Using split()

This method converts a snake case string into camel case by splitting the string into words, capitalizing subsequent words and joining them back together.

Python
s1= "geeksforgeeks_is_best"

# Split the snake_case string into a list of words
s2 = s1.split('_')

# Capitalize each word after the first and join them back together
res = s2[0] + ''.join(word.capitalize() for word in s2[1:])
print(res)

Output
geeksforgeeksIsBest

Explanation:

  • s.split('_'): This splits the string by underscores into a list of words.
  • word.capitalize(): This capitalizes each word after the first.
  • ''.join(...): This joins the capitalized words without spaces.
  • temp[0] + ''.join(...): This combines the first word with the capitalized ones to form a camelCase string.

Using re.sub()

This method uses regular expressions to convert a snake case string to camel case by directly identifying underscores followed by lowercase letters and capitalizing them. It avoids splitting and joining, making it efficient for long strings with many underscores.

Python
import re

s= "geeksforgeeks_is_best"

# Use re.sub to replace underscores followed by lowercase letters
res = re.sub(r'_([a-z])', lambda match: match.group(1).upper(),s)
print(res)

Output
geeksforgeeksIsBest

Explanation:

  • re.sub(r'_([a-z])', ...): This finds underscores followed by lowercase letters.
  • lambda match: match.group(1).upper(): This capitalizes the letter following each underscore.

Using List comprehension

This method splits the snake case string, capitalizes each word after the first and joins them using list comprehension, offering a concise, efficient and readable way to convert to camel case.

Python
s = "geeksforgeeks_is_best"

# Split and capitalize each part after the first, then join them
res = ''.join([s.split('_')[0]] + [word.capitalize() for word in s.split('_')[1:]])
print(res)

Output
geeksforgeeksIsBest

Explanation:

  • split('_'): This splits the string into parts by underscores.
  • List comprehension: This capitalizes words after the first.
  • join(): This combines the parts into a single camel case string.

Using str.title()

This method simplifies the conversion by replacing underscores with spaces, capitalizing each word with title() and removing spaces. first letter is then converted to lowercase to maintain camel case.

Python
s = "geeksforgeeks_is_best"

temp = s.replace('_', ' ').title().replace(' ', '')
res = temp[0].lower() + temp[1:]

print(res)

Output
geeksforgeeksIsBest

Explanation:

  • replace('_', ' '): This replaces underscores with spaces, converting “geeksforgeeks_is_best” to “geeksforgeeks is best”.
  • title(): This capitalizes the first letter of each word, resulting in “Geeksforgeeks Is Best”.
  • replace(' ', ''): This removes the spaces, turning it into “GeeksforgeeksIsBest”.
  • temp[0].lower(): This converts the first letter to lowercase to ensure camel case format.
  • temp[1:]: This combines the remaining part of the string with the lowercase first letter to form the final camel case string.




Next Article
Practice Tags :

Similar Reads