Python中,我們在預定義某類具有相似格式的變量或者輸出一句含有多個變量的提示語句時,往往用到占位符,而占位符有兩種表達方式:
%方式:
下面這段代碼摘自matplotlib的_init_.py文件中class RcParams的定義:
1 msg_depr = "%s is deprecated and replaced with %s; please use the latter." 2 msg_depr_set = ("%s is deprecated. Please remove it from your " 3 "matplotlibrc and/or style files.") 4 msg_depr_ignore = "%s is deprecated and ignored. Use %s instead." 5 msg_obsolete = ("%s is obsolete. Please remove it from your matplotlibrc " 6 "and/or style files.") 7 msg_backend_obsolete = ("The {} rcParam was deprecated in version 2.2. In" 8 " order to force the use of a specific Qt binding," 9 " either import that binding first, or set the " 10 "QT_API environment variable.")
其中,%s表示此展位符可用相關字符串代替,這是一個比較簡單的實例,%方式的占位符具體格式為:
%[(name)][flags][width].[precision]typecode
- (name) 可選,用於選擇指定的key
- flags 可選,可供選擇的值有:
- + 右對齊;正數前加正好,負數前加負號;
- - 左對齊;正數前無符號,負數前加負號;
- 空格 右對齊;正數前加空格,負數前加負號;
- 0 右對齊;正數前無符號,負數前加負號;用0填充空白處
- width 可選,占有寬度
- .precision 可選,小數點后保留的位數
- typecode 必選
- s,獲取傳入對象的__str__方法的返回值,並將其格式化到指定位置
- r,獲取傳入對象的__repr__方法的返回值,並將其格式化到指定位置
- c,整數:將數字轉換成其unicode對應的值,10進制范圍為 0 <= i <= 1114111(py27則只支持0-255);字符:將字符添加到指定位置
- o,將整數轉換成 八 進制表示,並將其格式化到指定位置
- x,將整數轉換成十六進制表示,並將其格式化到指定位置
- d,將整數、浮點數轉換成 十 進制表示,並將其格式化到指定位置
- e,將整數、浮點數轉換成科學計數法,並將其格式化到指定位置(小寫e)
- E,將整數、浮點數轉換成科學計數法,並將其格式化到指定位置(大寫E)
- f, 將整數、浮點數轉換成浮點數表示,並將其格式化到指定位置(默認保留小數點后6位)
- F,同上
- g,自動調整將整數、浮點數轉換成 浮點型或科學計數法表示(超過6位數用科學計數法),並將其格式化到指定位置(如果是科學計數則是e;)
- G,自動調整將整數、浮點數轉換成 浮點型或科學計數法表示(超過6位數用科學計數法),並將其格式化到指定位置(如果是科學計數則是E;)
- %,當字符串中存在格式化標志時,需要用 %%表示一個百分號
注:Python中百分號格式化是不存在自動將整數轉換成二進制表示的方式
tpl = "i am %s" % "alex" print(tpl)
tpl = "i am %s age %d" % ("alex", 18) print(tpl)
tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18} print(tpl)
tpl = "percent %.2f" % 99.97623 print(tpl)
tpl = "i am %(pp).2f" % {"pp": 123.425556, } print(tpl)
tpl = "i am %(pp).2f %%" % {"pp": 123.425556, } print(tpl)
i am alex
i am alex age 18
i am alex age 18
percent 99.98
i am 123.43
i am 123.43 %
format方式:
以下是同時輸出字典roll_dict的key-value對的format表達方式:
total_times = 1000
roll_dict={"1":56, "2":65, "3":458, "4":25}
for i, result in roll_dict.items():
print('點數是{}的次數是:{},頻率是:{}' .format(i, result, result/total_times))
點數是1的次數是:56,頻率是:0.056
點數是2的次數是:65,頻率是:0.065
點數是3的次數是:458,頻率是:0.458
點數是4的次數是:25,頻率是:0.025
如上述例子所示:forma占位需要結合{}使用,{}中可以帶入相關變量的相關參數,上述例子中{}是空白,未指定類型,則默認是None,表示實際變量i,result,result/total_times依次取代前面三個{}。
format的{}格式具體如下:
[[fill]align][sign][#][0][width][,][.precision][type]
- fill 【可選】空白處填充的字符
- align 【可選】對齊方式(需配合width使用)
- <,內容左對齊
- >,內容右對齊(默認)
- =,內容右對齊,將符號放置在填充字符的左側,且只對數字類型有效。 即使:符號+填充物+數字
- ^,內容居中
- sign 【可選】有無符號數字
- +,正號加正,負號加負;
- -,正號不變,負號加負;
- 空格 ,正號空格,負號加負;
- # 【可選】對於二進制、八進制、十六進制,如果加上#,會顯示 0b/0o/0x,否則不顯示
- , 【可選】為數字添加分隔符,如:1,000,000
- width 【可選】格式化位所占寬度
- .precision 【可選】小數位保留精度
- type 【可選】格式化類型
- 傳入” 字符串類型 “的參數
- s,格式化字符串類型數據
- 空白,未指定類型,則默認是None,同s
- 傳入“ 整數類型 ”的參數
- b,將10進制整數自動轉換成2進制表示然后格式化
- c,將10進制整數自動轉換為其對應的unicode字符
- d,十進制整數
- o,將10進制整數自動轉換成8進制表示然后格式化;
- x,將10進制整數自動轉換成16進制表示然后格式化(小寫x)
- X,將10進制整數自動轉換成16進制表示然后格式化(大寫X)
- 傳入“ 浮點型或小數類型 ”的參數
- e, 轉換為科學計數法(小寫e)表示,然后格式化;
- E, 轉換為科學計數法(大寫E)表示,然后格式化;
- f , 轉換為浮點型(默認小數點后保留6位)表示,然后格式化;
- F, 轉換為浮點型(默認小數點后保留6位)表示,然后格式化;
- g, 自動在e和f中切換
- G, 自動在E和F中切換
- %,顯示百分比(默認顯示小數點后6位)
- 傳入” 字符串類型 “的參數
1 tpl = "i am {}, age {}, {}".format("seven", 18, 'alex') 2 print(tpl) 3 4 tpl = "i am {}, age {}, {}".format(*["seven", 18, 'alex']) 5 print(tpl) 6 7 tpl = "i am {0}, age {1}, really {0}".format("seven", 18) 8 print(tpl) 9 10 tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18]) 11 print(tpl) 12 13 tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18) 14 print(tpl) 15 16 tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18}) 17 print(tpl) 18 19 tpl = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33]) 20 print(tpl) 21 22 tpl = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1) 23 print(tpl) 24 25 tpl = "i am {:s}, age {:d}".format(*["seven", 18]) 26 print(tpl) 27 28 tpl = "i am {name:s}, age {age:d}".format(name="seven", age=18) 29 print(tpl) 30 31 tpl = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18}) 32 print(tpl) 33 34 tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2) 35 print(tpl) 36 37 tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2) 38 print(tpl) 39 40 tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15) 41 print(tpl) 42 43 tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15) 44 print(tpl)
i am seven, age 18, alex
i am seven, age 18, alex
i am seven, age 18, really seven
i am seven, age 18, really seven
i am seven, age 18, really seven
i am seven, age 18, really seven
i am 1, age 2, really 3
i am seven, age 18, money 88888.100000
i am seven, age 18
i am seven, age 18
i am seven, age 18
numbers: 1111,17,15,f,F, 1587.623000%
numbers: 1111,17,15,f,F, 1587.623000%
numbers: 1111,17,15,f,F, 1500.000000%
numbers: 1111,17,15,f,F, 1500.000000%
從上述例子中 可以看出{}在語句中出現的位置依次與format中變量出現的位置從左到右依次對應,但如果{}中有整型數據,則整型數據值表示format中元素的索引位置:
1 tpl = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33]) 2 print(tpl)
輸出為:
i am 1, age 2, really 3
如果改成:
1 tpl = "i am {0}, age {1[1]}, really {1[2]}".format([1, 2, 3], [11, 22, 33]) 2 print(tpl)
則輸出為:
i am [1, 2, 3], age 22, really 33
此種方式更顯靈活。