Python: Convert a given string to Snake case
Python String: Exercise-97 with Solution
Snake case (stylized as snake_case) refers to the style of writing in which each space is replaced by an underscore (_) character, and the first letter of each word written in lowercase. It is a commonly used naming convention in computing, for example for variable and subroutine names, and for filenames. One study has found that readers can recognize snake case values more quickly than camel case.
Write a Python program to convert a given string to Snake case.
- Use re.sub() to match all words in the string, str.lower() to lowercase them.
- Use re.sub() to replace any - characters with spaces.
- Finally, use str.join() to combine all words using - as the separator.
java_script foo_bar foo_bar foo.bar foo_bar foo_bar foo_bar
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 convert a given string to camelcase.
Next: Write a Python program to decapitalize the first letter of a given string.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Python: Tips of the Day
Returns the symmetric difference between two iterables, without filtering out duplicate values:
def tips_symmetric_difference(p, q): _p, _q = set(p), set(q) return [item for item in p if item not in _q] + [item for item in q if item not in _p] print(tips_symmetric_difference([2, 4, 6], [2, 4, 8]))
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook