I'm able to find a bevy information online (in stackoverflow and otherwise) about how it's a very inefficient and bad practice to use
Lets say you have 3 strings:
'foo' 'bar' 'baz'
When you use
+=, python first needs to allocate and create:
before it can finally allocate and create:
So for each
+= that gets called, the entire contents of the string and whatever is getting added to it need to be copied into an entirely new memory buffer. In other words, if you have
N strings to be joined, you need to allocate approximately
N temporary strings and the first substring gets copied ~N times. The last substring only gets copied once, but on average, each substring gets copied
.join, python can figure out how much memory it needs up-front and then allocate a correctly sized buffer. Finally, it then copies each piece into the new buffer which means that each piece is only copied once.