python中可以对string, int, float等数据类型进行格式化操作。下面举例来说明一些常用操作。
先贴出 python 对 String Formatting Operations 讲解的连接,后面的例子和内容都以它为参考。
- flags
‘#‘ :
‘0‘ : 用‘0‘进行填充
‘-‘ : 左对齐
‘ ‘ : 对于数字来说,整数前面会有个空格,负数不收到影响
‘+‘ : 对数字添加正负号
- conversion list

In[101]: print ‘%30.4fabc‘ % -1.23456
-1.2346abc
In[102]: print ‘%30.4fabc‘ % -13345.3456
-13345.3456abc
In[103]: print ‘%-30.4fabc‘ % -13345.3456
-13345.3456 abc
In[104]: print ‘%-030.4fabc‘ % -13345.3456
-13345.3456 abc
In[105]: print ‘%030.4fabc‘ % -13345.3456
-000000000000000000013345.3456abc
In[106]: print ‘%30sabc‘ % ‘hello,‘
hello,abc
In[107]: print ‘%-30sabc‘ % ‘hello,‘
hello, abc
In[108]: print ‘% d‘ % -10
-10
In[109]: print ‘% d‘ % 10
10
In[111]: print("%#x" % -11)
-0xb
In[112]: print("%0x" % -11)
-b
原文:http://www.cnblogs.com/chen-kh/p/7251648.html