by harryf · 10y ago · 0 comments
'%s %s' % ('one', 'two')
With '%s %s'.format('one', 'two')
The latter is just more annoying to type. Stupid argument I know but I find myself grumbling to myself every time... '{} {}'.format('one', 'two')
Either way, the former example still works in Python 3.5, so that syntax hasn't gone away. `format` is preferred, though. This Stack Overflow question has some good answers as to why: http://stackoverflow.com/questions/5082452/python-string-for... '{} {}'.format('one', 'two')
Your experience may vary, but in my experience, when I switched to .format(), I found a number of bugs in code that used % instead. As mentioned, you can continue using %.I love love love printf() and it's ilk, so switching to something else (no matter how well designed) seemed asinine at first.
But .format() has really started to grow on me and did uncover some subtle bugs in old code.
'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W') f'{one} {two}'https://docs.python.org/2/library/string.html#string.Templat...