How does string concatenation work?



In the context of this exercise, how does string concatenation work?


In string concatenation, each time a string is added, Python allocates memory to fit the total string, and then copies all the strings into that location in memory. So, if you were adding multiple strings together, like

string1 + string2 + ... + stringN

It would do this process during each concatenation, always adding from the very first string each time

string1 + string2
string1 + string2 + string3

until it reaches the last added string.

As a result, concatenation with many strings can be inefficient, but for most problems that we will encounter, the time it takes will usually be negligible.