Python的字符串格式化有兩種方式: 百分號方式、format方式
百分號的方式相對來說比較老,而format方式則是比較先進的方式,企圖替換古老的方式,目前兩者並存。[PEP-3101]
This PEP proposes a new system for built-in string formatting operations, intended as a replacement for the existing '%' string formatting operator.
1、百分號方式
- (name) 可選,用於選擇指定的key
- flags 可選,可供選擇的值有:width 可選,占有寬度
- + 右對齊;正數前加正好,負數前加負號;
- - 左對齊;正數前無符號,負數前加負號;
- 空格 右對齊;正數前加空格,負數前加負號;
- 0 右對齊;正數前無符號,負數前加負號;用0填充空白處
- .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中百分號格式化是不存在自動將整數轉換成二進制表示的方式
常用格式化:
1 tpl = "i am %s" % "alex" 2 3 tpl = "i am %s age %d" % ("alex", 18) 4 5 tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18} 6 7 tpl = "percent %.2f" % 99.97623 #打印浮點數 8 9 tpl = "i am %(pp).2f" % {"pp": 123.425556, } 10 11 tpl = "i am %.2f %%" % {"pp": 123.425556, } #打開百分比
示例:
1、%s 可以字符串拼接
1 msg='i am %s my hobby is %s' % ('lhf','alex') 2 print(msg)
執行結果:
1 i am lhf my hobby is alex
2、%s 可以按收任何類型( 數字對應字符串)
1 msg='i am %s my hobby is %s' % ('lhf',1) 2 print(msg)
執行結果:
1 i am lhf my hobby is 1
3、%s 可以字符串接收列表
1 msg='i am %s my hobby is %s' % ('lhf',[1,2]) 2 print(msg)
執行結果:
1 i am lhf my hobby is [1, 2]
4、%s傳字符串或任何值, %d只能接收數字
1 name='lhf' 2 age=19 3 msg='i am %s my hobby is %d' % (name,age) #age可以用%d or %s,但用%s程序可讀性差 4 print(msg)
執行結果:
1 i am lhf my hobby is 19
5、%d 只能接收數字類型,不能接收列表
1 # %d 只能按收數字類型 2 msg='i am %s my hobby is %d' % ('lhf',1) 3 print(msg)
執行結果:
1 i am lhf my hobby is 1
6、%d 不能接收列表類型,會報錯
1 msg='i am %s my hobby is %d' % ('lhf',[1,2]) 2 print(msg)
執行結果:
1 Traceback (most recent call last):
2 File "D:/python/day5/format_type.py", line 14, in <module>
3 msg='i am %s my hobby is %d' % ('lhf',[1,2])
4 TypeError: %d format: a number is required, not list
7、打印浮點數
1 tpl = "percent %.2f" % 99.976234444444444444 2 print(tpl)
執行結果:
1 percent 99.98
8、打印百分比
1 tpl = 'percent %.2f %%' % 99.976234444444444444 2 print(tpl)
執行結果:
1 percent 99.98 %
9、左邊打印空格
1 msg='i am %(name)+60s my hobby is alex' %{'name':'lhf'} 2 print(msg)
執行結果:
1 i am lhf my hobby is alex
10、打印空格加顏色(黃色)
1 msg='i am \033[43;1m%(name)+60s\033[0m my hobby is alex' %{'name':'lhf'} 2 print(msg)
執行結果:
1 i am lhf my hobby is alex
19、不用格式化的方式
1 print('root','x','0','0',sep=':') 2 print('root'+':'+'x'+':'+'0','0')
執行結果:
1 root:x:0:0 2 root:x:0 0
2、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 3 #必須一一對應,否則會報錯 4 tpl = "i am {}, age {}, {}".format(*["seven", 18, 'alex']) 5 6 7 tpl = "i am {0}, age {1}, really {0}".format("seven", 18) 8 9 10 tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18]) 11 12 13 tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18) 14 15 16 tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18}) 17 ** 代表傳字典 18 19 tpl = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33]) 20 21 22 tpl = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1) 23 s 代表字符串 d 代表整數 24 25 tpl = "i am {:s}, age {:d}".format(*["seven", 18]) 26 * 代表列表 27 28 tpl = "i am {name:s}, age {age:d}".format(name="seven", age=18) 29 30 31 tpl = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18}) 32 33 34 tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2) 35 2進制 8進制 10進制 x與X: 16進制 %:百分比 36 37 tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2) 38 39 40 tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15) 41 42 43 tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15)
示例:
ps1:
1 tpl = "i am {0}, age {1}, really {2}".format("seven", '18','alex') 2 print(tpl)
執行結果:
1 i am seven, age 18, really alex
ps2:
1 tpl = "i am {2}, age {1}, really {0}".format("seven", '18','alex') 2 print(tpl)
執行結果:
1 i am alex, age 18, really seven
ps3: 傳字典的方式
1 tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18) 2 print(tpl)
執行結果:
1 i am seven, age 18, really seven
ps4: ** 傳字典的方式 (跟ps3的方式是一樣的)
1 tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18}) 2 print(tpl)
執行結果:
1 i am seven, age 18, really seven
ps5: * 代表:列表
1 tpl = "i am {:s}, age {:d}".format(*["seven", 18]) 2 print(tpl)
執行結果:
1 i am seven, age 18
ps6:
1 tpl = "i am {:s}, age {:d}".format("seven", 18) #["seven", 18] 2 print(tpl)
執行結果:
1 i am seven, age 18
ps7:
1 l=["seven", 18] 2 tpl = "i am {:s}, age {:d}".format('seven',18) 3 print(tpl)
執行結果:
1 i am seven, age 18
ps8: 2進制 8進制 10進制 x與X: 16進制 %:百分比
1 tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%},{}".format(15, 15, 15, 15, 15, 15.87623, 2) 2 print(tpl)
執行結果:
1 numbers: 1111,17,15,f,F, 1587.623000%,2
更多格式化操作:https://docs.python.org/3/library/string.html