它有着丰富的的格式限定符(语法是{}中带:号),比如:

填充与对齐
填充常跟对齐一起使用
^
<>分别是居中、左对齐、右对齐,后面带宽度
:
号后面带填充的字符,只能是一个字符,不指定的话默认是用空格填充
比如

In [15]: '{:>8}'.format('189')
Out[15]: '     189'
In [16]: '{:0>8}'.format('189')
Out[16]: '00000189'
In [17]: '{:a>8}'.format('189')
Out[17]: 'aaaaa189'

精度与类型f
精度常跟类型f一起使用

In [44]: '{:.2f}'.format(321.33345)
Out[44]: '321.33'

其中.2表示长度为2的精度,f表示float类型。

其他类型
主要就是进制了,bdox分别是二进制、十进制、八进制、十六进制。

In [54]: '{:b}'.format(17)
Out[54]: '10001'
In [55]: '{:d}'.format(17)
Out[55]: '17'
In [56]: '{:o}'.format(17)
Out[56]: '21'
In [57]: '{:x}'.format(17)
Out[57]: '11'

用,号还能用来做金额的千位分隔符。

In [47]: '{:,}'.format(1234567890)
Out[47]: '1,234,567,890'

 

 

Ref:

http://blog.csdn.net/handsomekang/article/details/9183303

 

________________________________________ 3S CONFIDENTIALITY NOTICE: This message and all attachments may contain legally privileged and confidential information. Any unauthorized review, use or distribution by anyone other than the intended recipient is strictly prohibited. If you are not the intended recipient, please contact the sender immediately by replying to the message and delete all copies. Thank you.
arrow
arrow
    全站熱搜

    天才R 發表在 痞客邦 留言(0) 人氣()