Normalize Text
Checking your account…
Sign in to save your code and progress across devices. The lesson and problem statement remain public.
Loading the interactive Practice workspace.If it does not appear, the problem and learning material remain readable, but browser execution is unavailable.Reload Practice workspace
Problem
Implement normalize_text(text). Return lowercase words joined by one space, with leading, trailing, and repeated whitespace removed.
Starter code
def normalize_text(text):
passTest cases
sample
{
"args": [
" Hello PYTHON "
]
}Expected: "hello python"
Wizard outline
- Step 1: Normalize into word tokens
Remove edge whitespace, collapse all whitespace runs, and lowercase the words. split() without an explicit separator handles spaces, tabs, and newlines while discarding empty fields.
- Step 2: Join with one canonical separator
Return normalized words separated by exactly one space. Joining the normalized token list guarantees no leading, trailing, or repeated whitespace.
Footguns and prerequisites
- strip removes only edge whitespace and does not collapse internal runs.
- strings
Reviewed references
Recommended approach and implementation
Lowercase, split on arbitrary whitespace, then join with one space.
Why it works: split removes every whitespace run and join inserts exactly one separator, while lower normalizes case.
def _normalized_tokens(text):
return text.lower().split()
def normalize_text(text):
return ' '.join(_normalized_tokens(text))