0

Is there an easy way to set the default float presentation for Python's format command:

"{:5.3e}".format(a) 

so that if the variable a has a value of None instead of a float, some default like 5 spaces might be printed?

The format string can be include many fields and is given by a user. The values in a are calculated internally.

TrailDreaming
  • 507
  • 1
  • 5
  • 16
  • 1
    You could write a custom formatter which performs this test, see e.g. http://stackoverflow.com/questions/19864302/add-custom-conversion-types-for-string-formatting – toting Jan 27 '16 at 16:42

1 Answers1

0

I would suggest something like

print("{:.3e}".format(a)) if a else print(" "*5).

you don't need 5.3e since in scientific notation you always get 5 digits because of .3e. Except you want to shift the whole text to the right. Then you could use {:>10.3e}.

Abufari
  • 78
  • 8