Suppose this string:
The fox jumped over the log.
Turning into:
Quite surprising - no one posted simple function which will be much faster than ALL other posted solutions. Here it goes:
def compactSpaces(s): os = "" for c in s: if c != " " or (os and os[-1] != " "): os += c return os