[docs] [issue37512] Error in the documentation about string concatenation

Dmitriy report at bugs.python.org
Fri Jul 5 17:33:18 EDT 2019

New submission from Dmitriy <mironovd at icloud.com>:

There is an error in documentation about string concatenation:


Common Sequence Operations
6. Concatenating immutable sequences always results in a new object. This means that building up a sequence by repeated concatenation will have a quadratic runtime cost in the total sequence length. To get a linear runtime cost, you must switch to one of the alternatives below:

- if concatenating str objects, you can build a list and use str.join() at the end or else write to an io.StringIO instance and retrieve its value when complete

It is not true for str objects anymore. Example using timeit module shows that time grows linearly:
>>> timeit('a+="a"', setup='a=""', number=10000)
>>> timeit('a+="a"', setup='a=""', number=100000)

But for bytes it is still right:
>>> timeit('a+=b"a"', setup='a=b""', number=10000)
>>> timeit('a+=b"a"', setup='a=b""', number=100000)

assignee: docs at python
components: Documentation
messages: 347390
nosy: dmitriym, docs at python
priority: normal
severity: normal
status: open
title: Error in the documentation about string concatenation
type: enhancement
versions: Python 3.7

Python tracker <report at bugs.python.org>

More information about the docs mailing list