[Python-Dev] Usage of += on strings in loops in stdlib

Steven D'Aprano steve at pearwood.info
Wed Feb 13 12:37:13 CET 2013

On 13/02/13 19:52, Larry Hastings wrote:

> I've always hated the "".join(array) idiom for "fast" string concatenation
>--it's ugly and it flies in the face of TOOWTDI. I think everyone should
>use "x = a + b + c + d" for string concatenation, and we should just make
>  that fast.

"".join(array) is much nicer looking than:

     # ridiculous and impractical for more than a few items
     array[0] + array[1] + array[2] + ... + array[N]


     # not an expression
     result = ""
     for s in array:
         result += s

or even:

     # currently prohibited, and not obvious
     sum(array, "")

although I will admit to a certain fondness towards

     # even less obvious than sum
     map(operator.add, array)

and join has been the obvious way to do repeated concatenation of many substrings since at least Python 1.5 when it was spelled "string.join(array [, sep=" "]).


More information about the Python-Dev mailing list