I need a neat way to print nothing or a number

Chris Angelico rosuav at gmail.com
Tue Mar 26 17:08:16 CET 2013

On Wed, Mar 27, 2013 at 2:50 AM,  <cl at isbd.net> wrote:
> What's a neat way to print columns of numbers with blanks where a number
> is zero or None?
> E.g. I want to output something like:-
>     Credit      Debit   Description
>     100.00              Initial balance
>     123.45              Payment for cabbages
>                 202.00  Telephone bill
> For each line I have either the credit or the debit amount and the other
> is 0 or None.  However you can't get number formatting (old or new) to
> output a blank for 0 and it barfs on None.

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.


More information about the Python-list mailing list