I need a neat way to print nothing or a number

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Tue Mar 26 13:06:10 EDT 2013


Chris Angelico <rosuav <at> gmail.com> writes:

>
> Try printing out this expression:
> 
> "%.2f"%value if value else ''
> 
> Without the rest of your code I can't tell you how to plug that in,
> but a ternary expression is a good fit here.
> 
> ChrisA
> 

Unfortunately, that's not working, but gives a TypeError: a float is required
when the first value evaluates to False.
Apparently it's not that easy to combine number formatting with logical
operators - the same happens with my idea ('{:.2f}').format(value or '').

Wolfgang







More information about the Python-list mailing list