Deviopath › Python

Normalize Spaces

Easy · Strings

User input is often messy. .strip() removes whitespace at both ends, .split() breaks text on any whitespace (spaces, tabs, newlines), and " ".join(...) puts the pieces back together with one space between them.

Write a function normalize_spaces(text) that removes leading and trailing whitespace and replaces every run of whitespace inside the text with a single space.

Next: Times Table Row