Given a string in camel case, write a Python program to convert the given string from camel case to snake case.
Input : GeeksForGeeks Output : geeks_for_geeks 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.
Method #2 : List comprehension
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.
Method #4 : Python Regex
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course