Suppose this string:
The fox jumped over the log.
Turning into:
I have to agree with Paul McGuire's comment. To me,
' '.join(the_string.split())
is vastly preferable to whipping out a regex.
My measurements (Linux and Python 2.5) show the split-then-join to be almost five times faster than doing the "re.sub(...)", and still three times faster if you precompile the regex once and do the operation multiple times. And it is by any measure easier to understand -- much more Pythonic.