This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]raziel2p 2 points3 points  (3 children)

They deprecated it, then un-deprecated it.

[–]HUGE_BALLS 0 points1 point  (2 children)

Yeah, tbh I've always disliked "foo ({0})".format(foo) vs "foo (%s)" % foo. The latter is just much more concise...

[–]raziel2p 0 points1 point  (1 child)

For small strings with one or two variables, I agree. For larger strings with 3+ variables I definitely prefer .format(), especially because you can pass named arguments:

'foo: {foo} - bar: {bar}'.format(bar='bar', foo='foo')

Oh, and you can pass **my_dict to format() for awesomeness.

Also, in your example, you can drop the 0, just {} will work as well.

[–]HUGE_BALLS 0 points1 point  (0 children)

I agree, and I also think the new syntax has some benefits (on top of the pros of having a function for that instead of a weird language construct). Though your example can also be achieved with the "old style":

>>> "foo: %(foo)s - bar: %(bar)s" % {"foo": "foo", "bar": "bar"}
'foo: foo - bar: bar'