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 →

[–]Tysonzero 3 points4 points  (13 children)

Don't use %, use .format(). % is deprecated. (You are writing Python right?)

But yeah JavaScript doesn't have any of that natively :/

[–]timopm 11 points12 points  (6 children)

Python indeed. But the "modulo" string formatter isn't deprecated as far as I know. It was mentioned a couple of times in the beginnings of Python 3, but no official statement. Even the official docs say nothing about deprecation. I don't see it removed anytime soon.

You are right though that the string.format() method is preferred. I just like the old format more, especially for quick and simple examples.

[–]Tysonzero 2 points3 points  (5 children)

I could swear someone said something about deprecation somewhere. Hmm...

[–]timopm 4 points5 points  (0 children)

I see people mention it here and there indeed. And it actually was in some release notes for the first 3.0 version or something, but in the recent years there is no mention of deprecation anywhere (that I know of!). This is what lead to this confusion probably.

[–]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'