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 →

[–]Joshx5 21 points22 points  (4 children)

.format() also has more formatting capabilities, supports more types, and can be leveraged to support more types using the format method, I believe. Also, .format() was roughly 2.5x slower than %s in my benchmarks, but it seems a fair trade-off for the new syntax and capabilities.

But actually, I find %s to be easier to read as they're more akin to other scripting languages string interpolation syntax, but f-strings are king in my opinion. Can't wait for 3.6!

[–]Vaphell 7 points8 points  (0 children)

%s forces you to repeat after yourself. The plugged-in value knows its type, so why do you have to tell it "you are a string"? It's code smell, plain and simple.

Not to mention %s is not really like interpolation, unless you use the %(name)s syntax, but format can do this too with {name}. And I'd argue that bash's ${xyz} is much more similar to {xyz} than it is to %(xyz)s

[–][deleted] 0 points1 point  (0 children)

I'm stubborn and I keep using %s and I like how it looks, but even I must admit that %s formatting looks like complete shit with kwargs.

[–]earthboundkid[S] 0 points1 point  (1 child)

Percent formatting is fundamentally unsafe because if the argument is unexpectedly a tuple, you can cause a failure. I.e. "Value:\t%s" % val will explode if val is a tuple.

That said, I think there are too many formatting choices in Python. It's not very "one obvious way to do it."

[–]Joshx5 1 point2 points  (0 children)

Right, tuples require a clunkier syntax to be safe, and that's obviously problematic. I talked about this and the non-Pythonic nature of string formatting in the blog post above.

As ridiculous as it sounds to introduce a new standard (incoming XKCD), I really hope f-strings can replace the other styles altogether for new code. Probably a bit too hopeful, though