w3resource
Python Exercises

Python: Convert camel case string to snake case string

Python Regular Expression: Exercise-36 with Solution

Write a python program to convert camel case string to snake case string.

Sample Solution:-

Python Code:

def camel_to_snake(text):
        import re
        str1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', text)
        return re.sub('([a-z0-9])([A-Z])', r'\1_\2', str1).lower()

print(camel_to_snake('PythonExercises'))

Sample Output:

python_exercises                                                                                              

Flowchart:

Flowchart: Regular Expression - Convert camel case string to snake case string.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to find all words which are at least 4 characters long in a string.
Next: Write a python program to convert snake case string to camel case string.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming