How to get rid of extra + sign in print statement, Python
<pre><code>print('2**', n, ' + ', sep='', end='')
</code></pre>

<p>Hi the print statement above is in a loop so the output ends up being </p>

<pre><code>2 ** 10 + 2 ** 7 + 2 ** 6 + 2 ** 4 + 2 ** 1 +
</code></pre>

<p>I need to get rid of the last plus in the statement but have no idea how to go about doing so.</p>

<p>If you separate the exponents, as you have probably done, you can use <a href="https://docs.python.org/3/library/stdtypes.html#str.join" rel="nofollow"><code>str.join()</code></a>:</p>

<pre><code>&gt;&gt;&gt; exponents = (10, 7, 6, 4, 1)
&gt;&gt;&gt; print(' + '.join('2**{}'.format(n) for n in exponents))
2**10 + 2**7 + 2**6 + 2**4 + 2**1
</code></pre>

<p>That will work in both Python 2 &amp; 3. You can also use the <code>print()</code> function with the <code>sep</code> argument:</p>

<pre><code>&gt;&gt;&gt; print(*('2**{}'.format(n) for n in exponents), sep=' + ')
2**10 + 2**7 + 2**6 + 2**4 + 2**1
</code></pre>

python python-3.x printing