-5

I've recently watched a Python tutorial in which the author kept juggling various Python string formatting expressions and I was struck by a thought:

Why are there so many string formatting flavours in Python?

  • There's the C language printf based approach (which, by the way, in various sources is cited as being deprecated and scheduled for removal but there's an abundance of its examples in the standard Python documentation (!)).
  • On the other hand, there's the C# derived .format() way of handling strings.
  • Finally, as of the advent of Python 3.6, we've been given the formatted f-string literal, known as the f-string.
  • Some also include the string module but that seems an overkill to me.

As I found out here, the first two aforementioned techniques both differ and overlap each other, depending on the use.

And last but not least, isn't this opulence of methods contradictory to one of Python's declarations from the import this?

There should be one-- and preferably only one --obvious way to do it.

Correct me if I'm wrong, but this is inherently "un-Pythonic" in a way.

Would readers offer their thoughts on this, preferably backed up with some relevant examples?

halfer
  • 19,824
  • 17
  • 99
  • 186
baduker
  • 19,152
  • 9
  • 33
  • 56
  • 4
    It's an evolution. The old ways were deemed to be inadequate so new ways were invented. The Python mantra shouldn't forbid progress and improvement. – Jonathon Reinhart Mar 10 '18 at 14:46
  • 1
    `prinf` is so deprecated I never knew it existed. – John Mar 10 '18 at 14:46
  • They weren't introduced at the same time. Far from it. I presume backwards compatibility is pretty important – roganjosh Mar 10 '18 at 14:46
  • 1
    `f"…"` uses the same format strings as `.format()`, so there are really only two kinds of format strings. The original intent after introducing `.format` was to deprecate %-formatting, but that didn’t happen. (Which is good.) – Ry- Mar 10 '18 at 14:47
  • The move toward Unicode by default reflected a broader movement within the industry. For one thing, Java adopted it and this helped entrench the standard. With 3.6 this is the only way, consider 2-3.6 a transition period. – John Mar 10 '18 at 14:49
  • @Ryan: No, they don't. `f'{1+2}'`works fine, `'{1+2}'.format()` is a KeyError. – Eric Duminil Mar 10 '18 at 14:50
  • @EricDuminil I think what Ryan was saying is that the _code_ is equivalent. [From PEP 498](https://www.python.org/dev/peps/pep-0498/#code-equivalence), the embedded values in an fstring are calling `__format__` with the provided expression and formatter, which is the same thing `str.format` does. – miradulo Mar 10 '18 at 15:12
  • @EricDuminil: What miradulo said. `f'{1+2:>5}' == '{:>5}'.format(1+2)` – Ry- Mar 10 '18 at 15:19
  • @Ryan: miradulo's explanation makes sense. I thought you meant that the syntax is exactly the same. – Eric Duminil Mar 10 '18 at 15:32

2 Answers2

2

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.

tripleee
  • 175,061
  • 34
  • 275
  • 318
-2

https://docs.python.org/2/whatsnew/2.6.html#pep-3101-advanced-string-formatting

Looks like it is about making the feature of string formatting more powerful.

From the link above : "In Python 3.0, the % operator is supplemented by a more powerful string formatting method, format(). Support for the str.format() method has been backported to Python 2.6."

  • 1
    My response was in alignment with Jonathon Reinhart's comment on evolution and addressing the inadequacy and supported by Python doc. Those who downvoted if could mention the reason , that would help me to participate meaningfully further .Thanks ! – user5613625 Mar 10 '18 at 15:33