如何在Python中使用.format()函数的多个选项
我看了Python3.3文档中关于.format()函数的部分,也在网上查了一些资料,但很遗憾,我只找到了一些关于“单一”选项的例子和解释。
这里有一个例子:
- 打印右对齐的内容
>>> print("this is a {:>30} test ".format(2.345345345345))
this is a 2.345345345345 test
- 只打印小数点后2位
>>> print("this is a {:.2f} test ".format(2.345345345345))
this is a 2.35 test
但是我想同时做到这两点,该怎么做呢?我已经尝试了各种不同的写法,但都没有成功。有没有人知道正确的写法?
2 个回答
1
就像这样:
print("this is a {:>30.2f} test ".format(2.345345345345))
2
试试这个
print("this is a {:>30.2f} test ".format(2.345345345345))
如果不确定,就试试那些显而易见的解决办法。