effectivepython icon indicating copy to clipboard operation
effectivepython copied to clipboard

Item #20: print with % format string instead of f-string

Open bslatkin opened this issue 5 years ago • 2 comments

This code snippet:

try:
    result = careful_divide(x, y)
except ValueError:
    print('Invalid inputs')
else:
    print('Result is %.1f' % result)

The print at the end should be print(f'Result is {result:.1f}')

bslatkin avatar Nov 12 '19 17:11 bslatkin

Hi Brett,

I think that should be:

print(f'Result is {result:.1f}')

apasciuta avatar Jan 10 '20 11:01 apasciuta

Yes I got it backwards, thanks!

bslatkin avatar Jun 01 '24 19:06 bslatkin