Python format 格式化函數:搭建數據分析體系83篇

Python format 格式化函數。

使用str.format對字符串進行格式化。

Python format 格式化函數:搭建數據分析體系83篇

過去是使用 %,如下所示:

#打印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


分享到:


相關文章: