4 ms·
To avoid str() calls with concatenation, use % formatting: '%s %s' % ('hello', 'world') Avoiding these implicit behaviors helps avoid bugs; it's worth it.
by andreasvc 12y ago
To avoid str() calls with concatenation, use % formatting:
'%s %s' % ('hello', 'world')
Avoiding these implicit behaviors helps avoid bugs; it's worth it.
I'd recommend going for Python 3 unless there's a specific reason (library) keeping you on 2.
- andybak 12y agoI'd recommend: '{} {}'.format('hello', 'world') But yes - either way - it solves the need to do str() when concatenating.
- andreasvc 12y agoI never understood the need for the new style of formatting, or the advantage. Fortunately % formatting is no longer getting deprecated.
- pyre 12y agoThe only downside that I've come across is when you do something like: "%s %s" % blah If a bug causes `blah` to be a string (rather than a tuple or a list), it will attempt to expand the string, possibly giving a confusing error.