On Fri, Apr 30, 2021 at 11:15 PM Valentin Berlier <berlier.v@gmail.com> wrote:
> You could say that f-strings are redundant because they can't do anything that str.format can't, but  they make it possible to shave off the static overhead of going through python's protocols and enable additional optimizations.

But that was not the primary motivator for adding them to the language. 

Nor is it the primary motivator for using them. I really like f-strings, and I have never even thought about their performance characteristics.

With regard to the possible performance benefits of “string comprehensions”: Python is already poorly performant when working with strings character by character. Which is one reason we have nifty string methods like .replace() and .translate. (And join). 

I’d bet that many (most?) potential “string comprehensions” would perform better if done with string methods, even if they were optimized.

Another note that I don’t think has been said explicitly— yes strings are Sequences, but they are a very special case in that they can contain only one type of thing: length-1 strings. Which massively reduces the possible kinds of comprehensions one might write, and I suspect most of those are already covered by string methods.

[actually, I think this is a similar point as that made by David Mertz)

-CHB

--
Christopher Barker, PhD (Chris)

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython