[Python-ideas] Briefer string format

Chris Angelico rosuav at gmail.com
Wed Jul 22 02:15:21 CEST 2015

On Wed, Jul 22, 2015 at 9:04 AM, Mike Miller <python-ideas at mgmiller.net> wrote:
> Second, there is an inconsistency in quoting of string dictionary keys.
> That's unfortunate, but the way format currently works.  Since f'' will be
> implemented on top, is not the quoting issue orthogonal to it?

So I guess the question is: Does f"..." have to be implemented on top
of str.format, or should it be implemented separately on top of
object.__format__? The two could be virtually indistinguishable
anyway. Something like this:

loc = "world"
print(f"Hello, {loc}!")
# becomes
loc = "world"
print("Hello, "+loc.__format__("")+"!")
# maybe with the repeated concat optimized to a join

With that, there's no particular reason for the specifics of .format()
key lookup to be retained. Want full expression syntax? Should be easy
- it's just a matter of getting the nesting right (otherwise it's a
SyntaxError, same as (1,2,[3,4) would be). Yes, it'll be a bit harder
for simplistic parsers to work with, but basically, this is no longer
a string literal - it's a compact syntax for string formatting and
concatenation, which is something I can definitely get behind.

REXX allowed abuttal for concatenation, so you could write something like this:

msg = "Hello, "loc"!"

Replace those interior quotes with braces, and you have an f"..."
string. It's not a string, it's an expression, and it can look up
names in its enclosing scope. Describe it alongside list
comprehensions, lambda expressions, and so on, and it fits in fairly

No longer -0.5 on this.


More information about the Python-ideas mailing list