Clean and Format Text
This problem gives you messy text and asks you to clean it up. The main jobs are trimming extra spaces from the beginning and end, then formatting the letters so the result has the right shape.
From the examples, the cleaned text should have its first letter uppercase and the rest lowercase. That means " Hello World " becomes "Hello world", and " PYTHON " becomes "Python". If the input is only spaces, the answer should end up as an empty string.
The order of the steps matters here. First remove the outside spaces. After that, fix the letter casing. If you do those actions in the wrong order, the final text may not match what the problem expects.
This is really a text-cleaning task, not a sorting task or a counting task. The answer should be one cleaned string that keeps the original words but removes the messy spacing and fixes the capitalization.
Example Input & Output
Extra spaces are trimmed and the text is capitalized.
Simple lowercase text is capitalized.
Uppercase input is properly capitalized after trimming.
Algorithm Flow

Solution Approach
This problem is really about applying a small sequence of Python string methods in the correct order. The final result depends on doing the cleanup steps one after another instead of treating them as unrelated operations.
The first step should be trimming the text. In Python, the usual tool is strip(), which removes leading and trailing whitespace. That matters because inputs like " Hello World " and " " both contain extra outside spaces that should not survive into the result.
After trimming, the next step is capitalization. The examples show that the intended format is: first character uppercase, remaining characters lowercase. Python's capitalize() method matches that rule well. For example, "PYTHON".capitalize() becomes "Python", and "Hello World".capitalize() becomes "Hello world".
A clean solution is therefore:
There is one nice detail here: if the stripped result is an empty string, calling capitalize() on it still safely returns an empty string. So there is no need for a complicated special case unless you want to make that branch explicit for readability.
You could also write this with an if check, such as returning "" when the stripped text is empty and otherwise capitalizing it. That version is perfectly valid. But using the built-in methods directly keeps the code shorter and still easy to read.
The important lesson is not just knowing the methods individually, but understanding why the order matters. If you capitalize first and then strip later, you may still get the right result in some cases, but the clean mental model is still "remove outside noise first, then format the remaining text."
This approach is strong because it mirrors the problem statement directly. First clean the outside spacing, then normalize the casing. The runtime is O(n) because the string has to be scanned as part of trimming and capitalization, and the extra space is also proportional to the size of the resulting string.
Best Answers
def process_text(text):
cleaned = text.strip()
if cleaned:
return cleaned[0].upper() + cleaned[1:].lower()
return cleanedComments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
