[Python-ideas] f-string "debug" conversion

Eric V. Smith eric at trueblade.com
Tue Oct 2 20:27:03 EDT 2018

This idea was proposed to me at the core sprints last month by Larry 
Hastings. I've discussed it with a few people, who seem generally 
positive about it, and we've tweaked it a little bit. I've spent some 
time implementing it, and I think it's doable. I thought I'd post it 
here for any additional feedback.

Here’s the idea: for f-strings, we add a !d conversion operator, which 
is superficially similar to !s, !r, and !a. The meaning of !d is: 
produce the text of the expression (not its value!), followed by an 
equal sign, followed by the repr of the value of the expression. So:

value = 10
s = 'a string!'
print(f'next: {value+1!d}')


next: value+1=11
s='a string!'

I’m not proposing this for str.format(). It would only really make sense 
for named arguments, and I don’t think 
print('{value!d}'.format(value=value) is much of a win.

The result is a string, so if you really wanted to, you could use a 
string formatting spec. So:


would produce:

*      value=10      *

Although I don’t think that would be very useful in general.

The mnemonic is !d for “debugging”. I’d wanted to use !=, because 
there’s an equal sign involved in the result, but = is the one character 
that can’t be used after ! (it’s “not equal” in expressions, and 
f-strings look specifically for that case). I also mentioned !!, but I 
think I prefer !d as being less confusing.

This would be used in debugging print statements, that currently end up 
looking like:


and would now be:


There have been discussions about ways to specify str() vs. repr(), 
using characters other than '=', adding spaces, etc. But they all end up 
over-complicating what should be a simple tool, not a Swiss Army knife.



More information about the Python-ideas mailing list