Given a string in camel case, write a Python program to convert the given string from camel case to snake case.
Examples:
Input : GeeksForGeeks Output : Lazyroar_for_Lazyroar Input : ThisIsInCamelCase Output : this_is_in_camel_case
Let’s see the different ways we can do this task.
Method #1 : Naive Approach
This is a naive implementation to convert camel case to snake case. First, we initialize a variable ‘res’ with an empty list and append first character (in lower case) to it. Now, Each time we encounter a Capital alphabet, we append ‘_’ and the alphabet (in lower case) to ‘res’, otherwise, just append the alphabet only.
Python3
# Python3 program to convert string # from camel case to snake case def change_case( str ): res = [ str [ 0 ].lower()] for c in str [ 1 :]: if c in ( 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' ): res.append( '_' ) res.append(c.lower()) else : res.append(c) return ''.join(res) # Driver code str = "GeeksForGeeks" print (change_case( str )) |
Lazyroar_for_Lazyroar
Time complexity: O(n)
Auxiliary Space: O(n), where n is length of given string.
Method #2 : List comprehension
Python3
# Python3 program to convert string # from camel case to snake case def change_case( str ): return ' '.join([' _' + i.lower() if i.isupper() else i for i in str ]).lstrip( '_' ) # Driver code str = "GeeksForGeeks" print (change_case( str )) |
Lazyroar_for_Lazyroar
Method #3 : Python reduce()
Python reduce() method applies a function to all the string alphabets, that wherever it find uppercase alphabet, it add ‘_’ in front of it and replace the uppercase alphabet with lowercase alphabet.
Python3
# Python3 program to convert string # from camel case to snake case from functools import reduce def change_case( str ): return reduce ( lambda x, y: x + ( '_' if y.isupper() else '') + y, str ).lower() # Driver code str = "GeeksForGeeks" print (change_case( str )) |
Lazyroar_for_Lazyroar
Method #4 : Python Regex
Python3
# Python3 program to convert string # from camel case to snake case import re def change_case( str ): s1 = re.sub( '(.)([A-Z][a-z]+)' , r '\1_\2' , str ) return re.sub( '([a-z0-9])([A-Z])' , r '\1_\2' , s1).lower() # Driver code str = "GeeksForGeeks" print (change_case( str )) |
Lazyroar_for_Lazyroar
Method #5 : Using isupper(),lower() and slicing
Python3
# Python3 program to convert string # from camel case to snake case def change_case( str ): res = "" for i in str : if (i.isupper()): res + = "_" + i.lower() else : res + = i return res[ 1 :] # Driver code str = "GeeksForGeeks" print (change_case( str )) |
Lazyroar_for_Lazyroar
Method #6 : Using recursion
The provided code consists of two functions: camel_to_snake and cameltosnake. The camel_to_snake function is a wrapper function that converts the first character of the input string to lowercase and then calls the cameltosnake function on the modified string.
The cameltosnake function converts the input string from camel case to snake case by recursively processing the string character by character. If the current character is uppercase, it adds an underscore before it and makes it lowercase. If the current character is lowercase, it simply returns it. The base case for the recursion is when the input string is empty, in which case the function simply returns an empty string. The result of the recursion is then returned by the camel_to_snake function.
Python3
def cameltosnake(camel_string: str ) - > str : # If the input string is empty, return an empty string if not camel_string: return "" # If the first character of the input string is uppercase, # add an underscore before it and make it lowercase elif camel_string[ 0 ].isupper(): return f "_{camel_string[0].lower()}{cameltosnake(camel_string[1:])}" # If the first character of the input string is lowercase, # simply return it and call the function recursively on the remaining string else : return f "{camel_string[0]}{cameltosnake(camel_string[1:])}" def camel_to_snake(s): if len (s)< = 1 : return s.lower() # Changing the first character of the input string to lowercase # and calling the recursive function on the modified string return cameltosnake(s[ 0 ].lower() + s[ 1 :]) # Example usage print (camel_to_snake( "GeeksForGeeks" )) # Output: "Lazyroar_for_Lazyroar" print (camel_to_snake( "ThisIsInCamelCase" )) # Output: "this_is_in_camel_case" #This code is contributed by Edula Vinay Kumar Reddy |
Lazyroar_for_Lazyroar this_is_in_camel_case
The time complexity of this approach is O(n), where n is the length of the input string. The auxiliary space is also O(n), since the recursive call stack will have a maximum depth of n.