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.
- Python - Convert Snake Case String to Camel Case
- Python - Convert Snake case to Pascal case
- PyQt5 - Snake Game
- Python regex to find sequences of one upper case letter followed by lower case letters
- Convert string to title case in Python
- Python | Convert case of elements in a list of strings
- Pandas - Convert the first and last character of each word to upper case in a series
- Python 3 | Program to print double sided stair-case pattern
- Python program to count upper and lower case characters without using inbuilt functions
- Python | Case Counter in String
- Python - Specific case change in String List
- Case-insensitive string comparison in Python
- Python - Case Insensitive string counter
- Python | Program to convert String to a List
- Python | Program to convert a tuple to a string
- Python program to convert a list to string
- Python program to convert hex string to decimal
- Python Program to convert List of Integer to List of String
- Python Program to Convert any Positive Real Number to Binary string
- Python program to split the string and convert it to dictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : shubham_singh