4 ms·
What makes me snarky is the replacing of '%s %s' % ('one', 'two') With '%s %s'.format('one', 'two') The latter is just more annoying to type. Stupid
by harryf 11y ago
What makes me snarky is the replacing of
'%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...
- davidism 11y agoIt's not replaced, both are perfectly valid. And this addition also exists in Python 2.
- RussianCow 11y agoThe second example should be: '{} {}'.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-formatting-vs-format http://stackoverflow.com/questions/5082452/python-string-for...
- dietrichepp 11y agoYou mean, '{} {}'.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 %.
- teach 11y agoI had the same experience. 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.
- gluelogic 11y agoThe thing about the latter form is that you can do something like '{0} {1} {0} {2}'.format('apple', 'banana', 'orange') which results in 'apple banana apple orange'
- squeaky-clean 11y agoYou can also use keyword arguments, which is really helpful in certain situation. Example from the docs: 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W')
- wyldfire 11y agoYou could do that before with % substitution too. But I prefer .format() because (1) it's the new idiom and (2) will coerce to string without me binding the type in the format string.
- mixmastamyk 11y agoSimplified to this in Py 3.6: f'{one} {two}'
- teddyh 11y agoDo you have a reference for that? Wouldn’t that be introducing a huge security hole in all programs?
- mixmastamyk 11y agoPEP 498. Literals only, does not add any additional security problems.
- teddyh 11y agoAh, right; I missed the ‘f’ prefix. And since it’s only done when parsing the expression, it is not a security problem. Thanks!
- kazagistar 11y agoIs it actually happening? Proper string interpolation? In all its extendable glory?
- mixmastamyk 11y agoPep was accepted, and I believe can be tried in a nightly or alpha release. It is not extensible though, GvR didn't see much utility, yet at least.
- frewsxcv 11y agoLet's not also forget string.Template ;) https://docs.python.org/2/library/string.html#string.Template https://docs.python.org/2/library/string.html#string.Templat...
- sevensor 11y agoI actually found a good use for string.Template, but that doesn't mean I understand why it exists in addition to the other two formatting sub-languages.
- mixmastamyk 11y agoIt was created long before .format and is better in some cases for i18n where simplicity is best.
- andreasvc 11y agoNo need to get snarky, because the % formatting is still in Python 3. It is also my preferred formatting method.
- RodericDay 11y agoThe super, super controversial automatic interpolation feature is what I'm really looking forward to.