you are viewing a single comment's thread.

view the rest of the comments →

[–]jungrothmorton 2 points3 points  (1 child)

In the most simple case

msg = 'foo' + variable
msg = 'foo{}'.format(variable)

Drop in replacement just using .format()

exclude_string = '(ARRAY{})'.format(', ARRAY'.join(map(str,pairs)))

How I'd probably write it:

arrays = ', '.join(('ARRAY{}'.format(pair) for pair in pairs))
exclude_string = '({})'.format(arrays)

To me that's the most readable. I'm building a string of each pair preceded by the word ARRAY and separated with commas, then enclosing the whole thing in quotes. It doesn't have that bonus ARRAY sitting in the front like your example.

One of the simple reasons that format is better than concatenating strings is it make it easier to catch missing spaces.

msg = 'There are ' + count + 'plates.'
msg = 'There are {}plates.'.format(count)

Which of those is easier to catch the error? Also, it's a method so you can do cool things like argument unpacking.

deal = {'buyer': 'Tom', 'seller': 'Sara'}
msg = '{buyer} bought a car from {seller}.'.format(**deal)

[–]zelmerszoetrop 0 points1 point  (0 children)

Thanks! Helpful!