Open In App

Python – Convert Snake Case String to Camel Case

Last Updated : 24 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a snake case string, convert to camel case.

Input : test_str = ‘geeksforgeeks_is_best_for_geeks’ 
Output : geeksforgeeksIsBestForGeeks 
Explanation : String converted to Camel Case.

Input : test_str = ‘geeksforgeeks_best_for_geeks’ 
Output : geeksforgeeksBestForGeeks 
Explanation : String converted to Camel Case. 

Method #1: Using split() + join() + title() + generator expression

The combination of above functions can be used to solve this problem. In this, we first split all underscores, and then join the string appending initial word, followed by title cased words using generator expression and title().

Python3




# Python3 code to demonstrate working of
# Convert Snake Case String to Camel Case
# Using split() + join() + title() + generator expression
 
# initializing string
test_str = 'geeksforgeeks_is_best'
 
# printing original string
print("The original string is : " + str(test_str))
 
# split underscore using split
temp = test_str.split('_')
 
# joining result
res = temp[0] + ''.join(ele.title() for ele in temp[1:])
     
# printing result
print("The camel case string is : " + str(res))


Output

The original string is : geeksforgeeks_is_best
The camel case string is : geeksforgeeksIsBest

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #2: Using  split() + join() + title() + map()

The combination of above functions can be used to solve this problem. In this, we perform the task of extending logic to entire strings using map(). 

Python3




# Python3 code to demonstrate working of
# Convert Snake Case String to Camel Case
# Using split() + join() + title() + map()
 
# initializing string
test_str = 'geeksforgeeks_is_best'
 
# printing original string
print("The original string is : " + str(test_str))
 
# saving first and rest using split()
init, *temp = test_str.split('_')
 
# using map() to get all words other than 1st
# and titlecasing them
res = ''.join([init.lower(), *map(str.title, temp)])
     
# printing result
print("The camel case string is : " + str(res))


Output

The original string is : geeksforgeeks_is_best
The camel case string is : geeksforgeeksIsBest

Time Complexity: O(n) as join function takes O(n)
Auxiliary Space: O(n)

Method #3: Using regex + lambda function

Use regex to split the string at every underscore and then use a lambda function to convert the first letter of every word to uppercase except for the first word. Finally, we can join the words to form the camel case string.

Step-by-step approach of the above idea:

  1. Import the ‘re‘ module for regular expression operations.
  2. Initialize the input string as ‘test_str‘.
  3. Print the original string.
  4. Use the ‘re.split()’ function to split the input string at every underscore. This creates a list of words from the input string.
  5. Create a lambda function that converts the first letter of each word to uppercase except for the first word.
  6. Use the ‘map()‘ function along with the lambda function to apply the conversion to each word in the list created in step 4.
  7. Join the list of words using the ”.join()” function to create the camel case string.
  8. Print the camel case string.

Below is the implementation of the above approach:

Python3




import re
 
# initializing string
test_str = 'geeksforgeeks_is_best'
 
# printing original string
print("The original string is : " + str(test_str))
 
# using regex to split string at every underscore
temp = re.split('_+', test_str)
 
# using lambda function to convert first letter of every word to uppercase except for first word
res = temp[0] + ''.join(map(lambda x: x.title(), temp[1:]))
 
# printing result
print("The camel case string is : " + str(res))


Output

The original string is : geeksforgeeks_is_best
The camel case string is : geeksforgeeksIsBest

Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n), where n is the length of the input string.

Method #4: Using for loop and conditional statements

Step-by-step approach:

  • Initialize an empty string variable result.
  • Iterate over every character in test_str.
  • If the current character is an underscore, set a flag variable capitalize_next to True. Otherwise, if capitalize_next is True, append the capitalized version of the current character to result and set capitalize_next to False.
  • Otherwise, simply append the current character to result.
  • Print the resulting camel case string.

Below is the implementation of the above approach:

Python3




# initializing string
test_str = 'geeksforgeeks_is_best'
 
# printing original string
print("The original string is : " + str(test_str))
 
# using for loop to convert string to camel case
result = ''
capitalize_next = False
for char in test_str:
    if char == '_':
        capitalize_next = True
    else:
        if capitalize_next:
            result += char.upper()
            capitalize_next = False
        else:
            result += char
 
# printing result
print("The camel case string is : " + str(result))


Output

The original string is : geeksforgeeks_is_best
The camel case string is : geeksforgeeksIsBest

Time Complexity: O(n), where n is the length of test_str.
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads