← Back to context

Comment by nickcw

3 days ago

From the f-strings PEP 498

https://peps.python.org/pep-0498/

> This PEP is driven by the desire to have a simpler way to format strings in Python.

I think f-strings have become the thing they were trying to replace and they are now more complicated than the old % interpolation.

I still like f-strings and use them a lot but if you look at the cheat sheet, they are no longer simple.

> if you look at the cheat sheet, they are no longer simple.

Most of the formatting options in the cheat sheet have always been available with f-strings. They are part of the Format Specification Mini Language (https://docs.python.org/3/library/string.html#format-specifi...) and both string.format and f-strings use the same spec. f-strings have some additional capabilities like inline expressions and debugging/logging with `=`.

  • The equals addition is basically the best thing ever, I almost always use it for logging. Incredibly helpful (for me, at least).

I don't think "simple" here means lack of functions. It means more intuitive and simpler code, and easier curve of learning. And to me f-string is very simple.

"Simpler" here is at least partly comparing to explicit calls to the .format method, which was added all the way back in 2.6.

%-style interpolation supports many of these features, they just weren't as well known or discussed back then. The % style is also more complicated because of the weird edge cases (like trying to interpolate a single value which is a tuple).

I think complexity is a byproduct of flexibility. At least in this case, there is a beginner version.