One line of code to remove all extra spaces before, after, and within a sentence: sentence = " The fox jumped over the log. " sentence = ' '.join(filter(None,sentence.split(' '))) Explanation: Split the entire string into a list. Filter empty elements from the list. Rejoin the remaining elements* with a single space More @Wikipedia
Hover over any link to get a description of the article. Please note that search keywords are sometimes hidden within the full article and don't appear in the description or title.