[New-bugs-announce] [issue40344] Programming FAQ about "What is the most efficient way to concatenate many strings together?" -- Improving the example

Dominik V. report at bugs.python.org
Mon Apr 20 17:27:30 EDT 2020


New submission from Dominik V. <dominik.vilsmeier1123 at gmail.com>:

The section mentions the usage of `str.join` and contains the following example:

    chunks = []
    for s in my_strings:
        chunks.append(s)
    result = ''.join(chunks)

Since `join` accepts any iterable the creation of the `chunks` list in a for loop is superfluous. If people just copy & paste from this FAQ they'll even end up with less performant code.

The example could be improved by providing an example list such as:

    strings = ['spam', 'ham', 'eggs']
    meal = ', '.join(strings)

Arguably this isn't a particularly long list of strings, so one more example could be added using e.g. `range(100)`:

    numbers = ','.join(str(x) for x in range(100))

This also emphasizes the fact that `join` takes any iterable rather than just lists.

----------
assignee: docs at python
components: Documentation
messages: 366887
nosy: Dominik V., docs at python
priority: normal
severity: normal
status: open
title: Programming FAQ about "What is the most efficient way to concatenate many strings together?" -- Improving the example
type: enhancement
versions: Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40344>
_______________________________________


More information about the New-bugs-announce mailing list