Drill

ProblemsPython › text

Tidy up pasted text

easytextPython

Text pasted from a spreadsheet arrives with tabs, newlines and runs of spaces in it. The search box wants one clean line.

normalise_space(text: string) → string

Solve it in the editor →

Where you start

def normalise_space(text: str) -> str:
    

Worked examples

CallResult
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())

The same problem in another language

More text problems in Python