1. Concatenating immutable strings always results in a new object. This means that building up a string by repeated concatenation will have a quadratic runtime cost in the total string length. To get a linear runtime cost, you must switch to one of the alternatives below:
  2. if concatenating str objects, you can build a list and use str.join() at the end;
  • if concatenating bytes objects, you can similarly use bytes.join(), or you can do in-place concatenation with a bytearray object. bytearray objects are mutable and have an efficient overallocation mechanism.

» Glossaire du langage python