Remove spaces before newlines
<p>I need to remove all spaces before the newline character throughout a string.</p>

<pre><code>string = """
this is a line       \n
this is another           \n
"""
</code></pre>

<p>output:</p>

<pre><code>string = """
this is a line\n
this is another\n
"""
</code></pre>

<p>You can <em>split</em> the string into lines, <em>strip</em> off all whitespaces on the right using <code>rstrip</code>, then add a new line at the end of each line:</p>

<pre><code>''.join([line.rstrip()+'\n' for line in string.splitlines()])
</code></pre>

python