Joining strings
For joining large numbers of strings, avoid using the + or += operators. Instead, the join() method should be preferred because it is substantially more efficient. This is because concatenation using the addition operator or it's augmented assignment version can lead to the generation of large numbers of temporaries, with consequent costs for memory allocation and copies. Let's see how join() is used.
The join() is a method on str which takes a collection of strings as an argument and produces a new string by inserting a separator between each of them. An interesting aspect of join() is how the separator is specified: it is the string on which join() is called.
As with many parts of Python, an example is the best explanation. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access