Suppose this string:
The fox jumped over the log.
Turning into:
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:
*The remaining elements should be words or words with punctuations, etc. I did not test this extensively, but this should be a good starting point. All the best!