Code Logo

Convert Phrase to Snake Case

Published at22 Apr 2026
Python String Handling Easy 0 views
Like0

You are given a string containing one or more words and need to convert it into snake_case.

That means the final result should use only lowercase words joined by underscores. Leading and trailing spaces should be removed first, and if the original text contains multiple spaces between words, they should collapse into a single underscore in the result.

For example, if text = " Hello World ", the answer should be "hello_world". If text = "Python String Handling", the result is "python_string_handling". A one-word input like "Single" becomes "single".

If the input is empty or contains only spaces, the result should be an empty string. So the task is to clean the spacing, lowercase every word, and join the words with underscores.

Example Input & Output

Example 1
Input
text = " "
Output
""
Explanation

A string with only spaces has no words, so the result is empty.

Example 2
Input
text = " Hello World "
Output
"hello_world"
Explanation

Extra spaces are ignored, both words are lowercased, and one underscore joins them.

Example 3
Input
text = "Python String Handling"
Output
"python_string_handling"
Explanation

Each word is converted to lowercase and the phrase becomes snake_case.

Algorithm Flow

Recommendation Algorithm Flow for Convert Phrase to Snake Case
Recommendation Algorithm Flow for Convert Phrase to Snake Case

Solution Approach

This problem is a very natural fit for Python string methods because each required transformation already has a built-in tool. The goal is to normalize spacing, lowercase the words, and connect them with underscores.

A clean way to think about it is in three steps. First, remove outside whitespace. Second, split the remaining text into words using whitespace as the separator. Third, join() those words back together with "_" after lowercasing them.

Python's split() is especially useful here because when it is called without an argument, it automatically treats any run of whitespace as a separator and ignores extra spaces at the ends. That means a string like " Hello World " becomes the word list ["Hello", "World"] without needing any manual cleanup of repeated spaces.

A concise solution is:

parts = text.split()
return "_".join(word.lower() for word in parts)

This works because split() already handles the spacing rule, the generator expression lowercases each word, and join() inserts exactly one underscore between consecutive words.

You could also write this as "_".join(text.lower().split()). That version is even shorter and is still completely correct for this problem. The key idea is the same in both versions: use Python's built-in string methods instead of manually scanning characters one by one.

Edge cases stay simple. If the input is empty or contains only spaces, split() returns an empty list, and joining an empty list produces an empty string. A single word becomes that word in lowercase with no underscores added.

This approach runs in O(n) time because the string is processed a constant number of times, and it uses O(n) extra space for the split words and final output string. More importantly, it is short, readable, and very idiomatic Python for text normalization tasks like this.

Best Answers

python - Approach 1
def convert_to_snake_case(text):
    return "_".join(text.lower().split())