Python format 格式化函數。
使用str.format對字符串進行格式化。
過去是使用 %,如下所示:
#打印python的系統環境變量
print('\n\nThe PYTHONPATH is %s\t%s' % (sys.path,'\n'))
現在用format可以增強字符串格式化的功能。
不設置指定位置,按默認順序
>>> "{} {}".format("hello", "world")
'hello world'
設置指定位置
>>> "{0} {1}".format("hello", "world")
'hello world'
設置指定位置
>>> "{1} {0} {1}".format("hello", "world")
'world hello world'
使用大括號 {} 來轉義大括號
print ("{} 對應的位置是 {{0}}".format("helloworld "))
輸出結果為:
helloworld 對應的位置是 {0}
更多參考:
http://www.runoob.com/python/att-string-format.html
閱讀更多 熱忱先生 的文章