[Solved] Why are there so many formatting flavours in Python?


This is what evolution looks like.

“We need string concatenation”.

Sure. "hello" + ''' ''' + 'world' or, if you like, ' '.join(['hello', 'world'])

“But "found " + str(var) + " matches" is kind of clumsy. I hear Lisp has princ…?”

Oh okay, here: 'found %i matches' % var

(… Time passes… )

“I hear you are overhauling the entire language and making print into a function. Gasp! Can we also fix this string formatting thing, it’s pretty crazy after all? Newbies don’t understand why you need parentheses when you interpolate more than one variable, and really, overriding the modulo operator for strings was a pretty wacky thing to do.”

Hm, you’re right, okay, here. 'found {0} matches'.format(var) and yeah, let’s get rid of that unintuitive complexity. We don’t want modulo to be a special case, it’s inelegant.

“Thanks. But… wait, there’s really a lot of code which uses the old syntax, and when you compare them, the new syntax is really clunky.”

Argh. We can make it less clumsy. Have some syntactic sugar! f'found {var} matches' and there’s more where that came from!

“Mmmm, I dunno. The modulo is still easier to type.”

You’re driving me nuts. Okay, you can play with it if you like. But don’t say I didn’t warn you.

solved Why are there so many formatting flavours in Python?