起步
在 Python 中,提供了很多種字符串格式化的方式,分別是 %-formatting、str.format 和 f-string 。本文將比較這幾種格式化方法。
%- 格式化
這種格式化方式來自于 C 語言風格的 sprintf 形式:
1
2
|
name = "weapon" "Hello, %s." % name |
C 語言的給實話風格深入人心,通過 % 進行占位。
為什么 %-formatting不好
不好的地方在于,如果字符串較長或較多的參數,那么可讀性就變得很差。
str.format 格式化
PEP-3101 帶來了 str.format ,它是對 %-formatting 的改進。它使用正常的函數調用語法,并且可以通過對要轉換為字符串的對象的 __format __() 方法進行擴展。
1
|
"Hello, {}. You are {}." . format (name, age) |
并支持字典形式傳參,免于位置參數帶來的麻煩:
1
|
"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__ 協議進行格式化。
1
2
3
|
name = "Eric" age = 74 f "Hello, {name}. You are {age}." |
語法上與 str.format() 類似,但更為簡潔,當字符串較長時也不會繁瑣。更強大的是它支持任意的表達式。我們可以在花括號內進行四則運算或函數調用等:f"{2 * 6}"
或者 f"{name.lower()} is funny"
。
并且它性能也最好。
幾種格式化方式性能比較
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
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) |
結果:
1
2
3
4
5
6
7
|
{ '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'
翻譯成:
1
2
3
4
5
6
7
8
|
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 中幾種字符串格式化方法及其比較,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
原文鏈接:https://www.hongweipeng.com/index.php/archives/1806/