python中有兩種格式化輸出字符串的方式:格式化表達式、format()方法。固然,還有一個簡化操做的內置format()函數。python
它們絕大部分功能都是重複的,熟悉printf的能夠考慮使用格式化表達式,不然使用format()更友好些,由於它像處理函數參數同樣,但format()有時候可能寫的要更復雜。函數
格式化表達式相似於printf的風格,在字符串中使用%
做爲佔位符。本文只是介紹python中的一些特性,若有須要請自行搜索printf用法。code
>>> who1 = "long" >>> who2 = "shuai" >>> "hello %s world" % "your" 'hello your world' >>> "hello %s world" % who1 'hello long world' >>> "hello %s world" % (who1) 'hello long world' >>> "hello %s %s world" % (who1, who2) 'hello long shuai world'
字符串和替換目標之間也使用%
分隔,且替換部分能夠有多個(使用括號包圍),能夠使用變量。orm
替換目標還能夠使用字典,這時在字符串中的%
佔位符能夠以key的方式來引用:索引
>>> "%(name1)s with %(name2)s" % {"name1":"longshuai", "name2":"xiaofang"} 'longshuai with xiaofang'
用字典的形式,能夠讓表達式格式化更模板化。例如:字符串
>>> reply = """ ... hello %(name)s! ... Your age is %(age)d""" >>> >>> values = {'name':"longshuai",'age':23} >>> print(reply % values) hello longshuai! Your age is 23
使用format()來格式化字符串時,使用在字符串中使用{}
做爲佔位符,佔位符的內容將引用format()中的參數進行替換。能夠是位置參數、命名參數或者兼而有之。it
看示例就明白了。ast
# 位置參數 >>> template = '{0}, {1} and {2}' >>> template.format('long','shuai','gao') 'long, shuai and gao' # 命名參數 >>> template = '{name1}, {name2} and {name3}' >>> template.format(name1='long', name2='shuai', name3='gao') 'long, shuai and gao' # 混合位置參數、命名參數 >>> template = '{name1}, {0} and {name3}' >>> template.format("shuai", name1='long', name3='gao') 'long, shuai and gao'
須要注意,format()函數中,位置參數必須放在全部的命名參數以前。例如,下面的會報錯:form
template.format(name1='long', "shuai", name3='gao')
由於字符串中的佔位符是直接引用format中的參數屬性的,在佔位符處能夠進行索引取值、方法調用等操做。例如:模板
>>> import sys >>> 'My {1[name]} OS is {0.platform}'.format(sys,{"name":"laptop"}) 'My laptop OS is win32' >>> 'My {config[name]} OS is {sys.platform}'.format(sys=sys,config={'name':'loptop'}) 'My loptop OS is win32'
可是,在佔位符使用索引或切片時,不能使用負數,但能夠將負數索引或負數切片放在format的參數中。
>>> s = "hello" >>> 'first={0[0]}, last={0[4]}'.format(s) 'first=h, last=o' # 下面是錯的 >>> 'first={0[0]}, last={0[-1]}'.format(s) # 下面是正確的 >>> 'first={0[0]}, last={1}'.format(s, s[-1]) 'first=h, last=o'
format()做爲函數,它也能進行參數解包,而後提供給佔位符。
>>> s=['a','b','c'] >>> '{0}, {1} and {2}'.format(*s) 'a, b and c'
在佔位符後面加上數值能夠表示佔用字符寬度。
>>> '{0:10} = {1:10}'.format('abc','def') 'abc = def ' >>> '{0:10} = {1:10}'.format('abc',123) 'abc = 123' >>> '{0:10} = {1:10}'.format('abc',123.456) 'abc = 123.456'
使用>
表示右對齊,<
表示左對齊,^
表示居中對齊,而且能夠使用0來填充空格。
>>> '{0:>10} = {1:>10}'.format('abc','def') ' abc = def' >>> '{0:>10} = {1:<10}'.format('abc','def') ' abc = def ' >>> '{0:^10} = {1:^10}'.format('abc','def') ' abc = def ' >>> '{0:10} , {1:<06}'.format('abc','def') 'abc , def000' >>> '{0:10} , {1:>06}'.format('abc','def') 'abc , 000def' >>> '{0:10} , {1:^06}'.format('abc','def') 'abc , 0def00'
能夠指定e、f、g類型的浮點數,默認採用g浮點數格式化。例如:
>>> '{0:f}, {1:.2f}, {2:06.2f}'.format(3.14159, 3.14159, 3.14159) '3.141590, 3.14, 003.14'
:.2f
表示保留兩位小數,:06.2f
表示最大長度位6字符,左邊使用0填充而不是字符串,保留2位小數。
甚至,能夠從format()中指定小數位數。
>>> '{0:.{1}f}'.format(1/3, 4) '0.3333'
除了字符串方法format(),還提供了一個快速格式化單個字符串目標的內置函數format()。
用法示例:
>>> '{0:.2f}'.format(1.2345) '1.23' >>> format(1.2345, '.2f') '1.23' >>> '%.2f' % 1.2345 '1.23'