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
test_str = 'geeksforgeeks_is_best'
print ( "The original string is : " + str (test_str))
temp = test_str.split( '_' )
res = temp[ 0 ] + ''.join(ele.title() for ele in temp[ 1 :])
print ( "The camel case string is : " + str (res))
|
OutputThe 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
test_str = 'geeksforgeeks_is_best'
print ( "The original string is : " + str (test_str))
init, * temp = test_str.split( '_' )
res = ''.join([init.lower(), * map ( str .title, temp)])
print ( "The camel case string is : " + str (res))
|
OutputThe 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:
- Import the ‘re‘ module for regular expression operations.
- Initialize the input string as ‘test_str‘.
- Print the original string.
- Use the ‘re.split()’ function to split the input string at every underscore. This creates a list of words from the input string.
- Create a lambda function that converts the first letter of each word to uppercase except for the first word.
- Use the ‘map()‘ function along with the lambda function to apply the conversion to each word in the list created in step 4.
- Join the list of words using the ”.join()” function to create the camel case string.
- Print the camel case string.
Below is the implementation of the above approach:
Python3
import re
test_str = 'geeksforgeeks_is_best'
print ( "The original string is : " + str (test_str))
temp = re.split( '_+' , test_str)
res = temp[ 0 ] + ''.join( map ( lambda x: x.title(), temp[ 1 :]))
print ( "The camel case string is : " + str (res))
|
OutputThe 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
test_str = 'geeksforgeeks_is_best'
print ( "The original string is : " + str (test_str))
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
print ( "The camel case string is : " + str (result))
|
OutputThe 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)