Self-documenting f-strings in Python allow you to write a print statement like this:
print(f"{result=}")
This works fine, but the =
sign is not just used to tell Python you want a self-documenting f-string...it is also the symbol that gets formatted into the output. Is there any way to get Python to use the :
character instead of the =
character in the output of self-documenting f-strings, maybe something like this:
print(f"{result:}")
No, it's not currently possible to change the way self-documenting f-strings are formatted. The documentation explains:
When the equal sign
'='
is provided, the output will have the expression text, the'='
and the evaluated value. Spaces after the opening brace'{'
, within the expression and after the'='
are all retained in the output. By default, the'='
causes therepr()
of the expression to be provided, unless there is a format specified. When a format is specified it defaults to thestr()
of the expression unless a conversion'!r'
is declared.
As you can see, the =
in the output is fixed by the specification.
I don't know the process of requesting changes to the language, but this seems like it would be a reasonable request. But for now you'll have to format it manually, like you've had to do before 3.12.