23. String concatenation
The key here is to avoid writing too much code just to concatenate a string. In the discouraged approach, note how we have to add logic to append a comma ,? In the encourage approach, the for loop is gone and there is no more need for when to add a comma.
23.1. Don’t do this
1names = ['john', 'jane', 'jeremy', 'janice', 'joyce', 'jonathan']
2
3s = ''
4for i, name in enumerate(names):
5 s += name
6 if i < len(names) - 1:
7 s += ', '
23.2. Do this
1names = ['john', 'jane', 'jeremy', 'janice', 'joyce', 'jonathan']
2
3s = ', '.join(names)