起步
在 Python 中,提供了很多種字符串格式化的方式,分別是 %-formatting、str.format 和 f-string 。本文將比較這幾種格式化方法。
%- 格式化
這種格式化方式來自于 C 語言風格的 sprintf 形式:
name = "weapon" "Hello, %s." % name
C 語言的給實話風格深入人心,通過 % 進行占位。
為什么 %-formatting不好
不好的地方在于,如果字符串較長或較多的參數,那么可讀性就變得很差。
str.format 格式化
PEP-3101 帶來了 str.format ,它是對 %-formatting 的改進。它使用正常的函數調用語法,并且可以通過對要轉換為字符串的對象的 __format __() 方法進行擴展。
"Hello, {}. You are {}.".format(name, age)
并支持字典形式傳參,免于位置參數帶來的麻煩:
"Hello, {name}. You are {age}.".format(name=name, age=age)
這兩種方式代碼效果相同,只是第一種方法需要嚴格控制傳入的參數位置,而第二種方法沒有這種限制, 并增加了代碼的可讀性。各種技巧可查看 Format Specification Mini-Language
為什么 str.format() 并不好
雖然它解決了字符串冗長情況下的可讀性,但需要對字典傳參基本是要重寫一遍變量名,不夠優雅。
f-string 格式化
PEP-0498 帶來了 f-string 方式,它從 Python3.6 開始支持。這種方式也是使用 __format__ 協議進行格式化。
name = "Eric" age = 74 f"Hello, {name}. You are {age}."
語法上與 str.format() 類似,但更為簡潔,當字符串較長時也不會繁瑣。更強大的是它支持任意的表達式。我們可以在花括號內進行四則運算或函數調用等:
f"{2 * 6}"
或者
f"{name.lower()} is funny"
。
并且它性能也最好。
幾種格式化方式性能比較
import timeit def add(): status = 200 body = 'hello world' return 'Status: ' + str(status) + '\r\n' + body + '\r\n' def old_style(): status = 200 body = 'hello world' return 'Status: %s\r\n%s\r\n' % (status, body) def formatter1(): status = 200 body = 'hello world' return 'Status: {}\r\n{}\r\n'.format(status, body) def formatter2(): status = 200 body = 'hello world' return 'Status: {status}\r\n{body}\r\n'.format(status=status, body=body) def f_string(): status = 200 body = 'hello world' return f'Status: {status}\r\n{body}\r\n' perf_dict = { 'add': min(timeit.repeat(lambda: add())), 'old_style': min(timeit.repeat(lambda: old_style())), 'formatter1': min(timeit.repeat(lambda: formatter1())), 'formatter2': min(timeit.repeat(lambda: formatter2())), 'f_string': min(timeit.repeat(lambda: f_string())), } print(perf_dict)
結果:
{ 'add': 0.8815229000000002, 'old_style': 0.6351808999999999, 'formatter1': 0.7536176999999995, 'formatter2': 1.2277180999999997, 'f_string': 0.4891379000000011 }
f-string 格式化的方式性能最好。
為何 f-string 速度如此快
從指令來看,
f'Status: {status}\r\n{body}\r\n'
翻譯成:
8 LOAD_CONST 3 ('Status: ') 10 LOAD_FAST 0 (status) 12 FORMAT_VALUE 0 14 LOAD_CONST 4 ('\r\n') 16 LOAD_FAST 1 (body) 18 FORMAT_VALUE 0 20 LOAD_CONST 4 ('\r\n') 22 BUILD_STRING 5
正如指令中所示的,f-string 是運行時渲染的,底層中轉成了類似 "Status: " + status+ "\r\n" + body + "\r\n" 的形式。正如 PEP-0498 中提到的:
F-strings provide a way to embed expressions inside string literals, using a minimal syntax. It should be noted that an f-string is really an expression evaluated at run time, not a constant value. In Python source code, an f-string is a literal string, prefixed with 'f', which contains expressions inside braces. The expressions are replaced with their values.
而其他方式則是要先創建字符串常量值,再進行替換之類的操作。
總結
我們仍然可以使用以前的方式進行格式化,但在此推薦 f-string 方式,因為它使用更簡潔,更易讀且更方便,性能又更好,完全沒理由拒絕啊。
從今天開始使用 f-string!
以上所述是小編給大家介紹的Python 中幾種字符串格式化方法及其比較,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
