Skip to content

Commit 62e59a5

Browse files
committed
Add code example to demonstrate proper string concatenation.
The text does a good job of explaining which route to take when concatenating strings, but the mention of "join" might mean nothing to beginners without a concrete example.
1 parent 50e6613 commit 62e59a5

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

docs/writing/structure.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,26 @@ its parts, it is much more efficient to accumulate the parts in a list,
391391
which is mutable, and then glue ('join') the parts together when the
392392
full string is needed.
393393

394+
**Bad**
395+
396+
.. code-block:: python
397+
398+
# create a concatenated string from 0 to 19 (e.g. "012..1819")
399+
nums = ""
400+
for n in range(20):
401+
nums += str(n) # slow and inefficient
402+
print nums
403+
404+
**Good**
405+
406+
.. code-block:: python
407+
408+
# create a concatenated string from 0 to 19 (e.g. "012..1819")
409+
nums = []
410+
for n in range(20):
411+
nums.append(str(n))
412+
print "".join(nums) # much more efficient
413+
394414
Vendorizing Dependencies
395415
------------------------
396416

0 commit comments

Comments
 (0)