Is there a way to set the default float presentation for Python "{:5.3e}".format(a) if a=None and is not a float?

时间:2016-01-27 16:05:40

标签: python string-formatting

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.

1 个答案:

答案 0 :(得分:0)

我会建议像

这样的东西
print("{:.3e}".format(a)) if a else print(" "*5).

您不需要5.3e,因为在科学记数法中,由于.3e,您总是得到5位数。除非您想将整个文本向右移动。然后你可以使用{:>10.3e}

相关问题