If you concatenate strings in a loop then the time taken by the loop is quadratic in the number of iterations.

Initialize an empty list before the start of the loop. During the loop append the substrings to the list. At the end of the loop, convert the list to a string by using ''.join(list).

  • Python Standard Library: The str.join method.
  • Python Frequently Asked Questions: What is the most efficient way to concatenate many strings together?.