What is the pythonic way to remove trailing spaces from a string?
<p>The parameter to the function satisfy these rules:</p>

<ol>
<li>It does not have any leading whitespace</li>
<li>It might have trailing whitespaces</li>
<li>There might be interleaved whitespaces in the string.</li>
</ol>

<p>Goal: remove duplicate whitespaces that are interleaved &amp; strip trailing whitespaces.</p>

<p>This is how I am doing it now:</p>

<pre><code># toks - a priori no leading space
def squeeze(toks):
  import re
  p = re.compile(r'\W+')
  a = p.split( toks ) 
  for i in range(0, len(a)):
    if len(a[i]) == 0:
      del a[i]
  return ' '.join(a) 

&gt;&gt;&gt; toks( '  Mary  Decker   is hot   ' )
Mary Decker is hot
</code></pre>

<p>Can this be improved ? Pythonic enough ?</p>
