Python的字符串格式化有兩種方式: 百分號方式、format方式html
百分號的方式相對來講比較老,而format方式則是比較先進的方式,企圖替換古老的方式,目前二者並存。[PEP-3101]python
This PEP proposes a new system for built-in string formatting operations, intended as a replacement for the existing '%' string formatting operator.ui
一、百分號方式spa
注:Python中百分號格式化是不存在自動將整數轉換成二進制表示的方式code
經常使用格式化:orm
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, } #打開百分比
示例:htm
一、%s 能夠字符串拼接對象
1 msg='i am %s my hobby is %s' % ('lhf','alex') 2 print(msg)
執行結果:blog
1 i am lhf my hobby is alex
二、%s 能夠按收任何類型( 數字對應字符串)ci
1 msg='i am %s my hobby is %s' % ('lhf',1) 2 print(msg)
執行結果:
1 i am lhf my hobby is 1
三、%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]
四、%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
五、%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
六、%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
七、打印浮點數
1 tpl = "percent %.2f" % 99.976234444444444444 2 print(tpl)
執行結果:
1 percent 99.98
八、打印百分比
1 tpl = 'percent %.2f %%' % 99.976234444444444444 2 print(tpl)
執行結果:
1 percent 99.98 %
九、左邊打印空格
1 msg='i am %(name)+60s my hobby is alex' %{'name':'lhf'} 2 print(msg)
執行結果:
1 i am lhf my hobby is alex
十、打印空格加顏色(黃色)
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
1九、不用格式化的方式
1 print('root','x','0','0',sep=':') 2 print('root'+':'+'x'+':'+'0','0')
執行結果:
1 root:x:0:0 2 root:x:0 0
二、Format 方式
[[fill]align][sign][#][0][width][,][.precision][type]
經常使用格式化:
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