Is python += string concatenation bad practice?
<p>I am reading <a href="http://docs.python-guide.org/en/latest/writing/structure/#mutable-and-immutable-types" rel="nofollow">The Hitchhikerâs Guide to Python</a> and there is a short code snippet </p>

<pre><code>foo = 'foo'
bar = 'bar'

foobar = foo + bar  # This is good
foo += 'ooo'  # This is bad, instead you should do:
foo = ''.join([foo, 'ooo'])
</code></pre>

<p>The author pointed out that <code>''.join()</code> is not always faster than <code>+</code>, so he is not against using <code>+</code> for string concatenation. </p>

<p>But why is <code>foo += 'ooo'</code> bad practice whereas <code>foobar=foo+bar</code> is considered good? </p>

<ul>
<li>is <code>foo += bar</code> good?</li>
<li>is <code>foo = foo + 'ooo'</code> good?</li>
</ul>

<p>Before this code snippet, the author wrote:</p>

<blockquote>
  <p>One final thing to mention about strings is that using join() is not always best. In the instances where you are creating a new string from a pre-determined number of strings, using the addition operator is actually faster, but in cases like above or in cases where you are adding to an existing string, using join() should be your preferred method.</p>
</blockquote>

<h2>Is it bad practice?</h2>

<p>It's reasonable to assume that it isn't bad practice for this example because:</p>

<ul>
<li>The author doesn't give any reason. Maybe it's just disliked by him/her.</li>
<li>Python documentation doesn't mention it's bad practice (from what I've seen).</li>
<li><code>foo += 'ooo'</code> is just as readable (according to me) and is approximately 100 times faster than <code>foo = ''.join([foo, 'ooo'])</code>.</li>
</ul>

<h2>When should one be used over the other?</h2>

<p>Concatenation of strings have the disadvantage of needing to create a new string and allocate new memory <em>for every concatenation</em>! This is time consuming, but isn't that big of a deal with few and small strings. When you know the number of strings to concatenate and don't need more than maybe 2-4 concatenations I'd go for it.</p>

<hr>

<p>When joining strings Python only has to allocate new memory for the final string, which is much more efficient, but could take longer to compute. Also, because strings are immutable it's often more practical to use a list of strings to dynamically mutate, and only convert it to a string when needed.</p>

<p>It's often convenient to create strings with str.join() since it takes an iterable. For example:</p>

<pre><code>letters = ", ".join("abcdefghij")
</code></pre>

<h2>To conclude</h2>

<p>In most cases it makes more sense to use <code>str.join()</code> but there are times when concatenation is just as viable. Using any form of string concatenation for huge or many strings would be bad practice just as using <code>str.join()</code> would be bad practice for short and few strings, in my own opinion.</p>

<p>I believe that the author was just trying to create a rule of thumb to easier identify when to use what without going in too much detail or make it complicated.</p>

python string string-concatenation