Tidy up pasted text
Text pasted from a spreadsheet arrives with tabs, newlines and runs of spaces in it. The search box wants one clean line.
- Every run of whitespace becomes a single space.
- No leading or trailing space is left.
- Text that is nothing but whitespace comes back empty.
normalise_space(text: string) → string
Where you start
def normalise_space(text: str) -> str:
Worked examples
| Call | Result |
|---|---|
normalise_space(" hello world ") | "hello world" |
normalise_space("a\tb\nc") | "a b c" |
normalise_space("already clean") | "already clean" |
normalise_space(" ") | "" |
Hint
Splitting on whitespace and joining with a single space does both jobs at once.
Reference solution in Python
def normalise_space(text: str) -> str:
return ' '.join(text.split())