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.
注:Python中百分號格式化是不存在自動將整數轉換成二進制表示的方式
常用格式化:
tpl = "i am %s" % "alex"
tpl = "i am %s age %d" % ("alex", 18)
tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18}
tpl = "percent %.2f" % 99.97623 #列印浮點數
tpl = "i am %(pp).2f" % {"pp": 123.425556, }
tpl = "i am %.2f %%" % {"pp": 123.425556, } #開啓百分比
範例:
1、%s 可以字串拼接
msg='i am %s my hobby is %s' % ('lhf','alex')
print(msg)
執行結果:
i am lhf my hobby is alex
2、%s 可以按收任何型別( 數位對應字串)
msg='i am %s my hobby is %s' % ('lhf',1)
print(msg)
執行結果:
i am lhf my hobby is 1
3、%s 可以字串接收列表
msg='i am %s my hobby is %s' % ('lhf',[1,2])
print(msg)
執行結果:
i am lhf my hobby is [1, 2]
4、%s傳字串或任何值, %d只能接收數位
'''
遇到問題沒人解答?小編建立了一個Python學習交流QQ羣:778463939
尋找有志同道合的小夥伴,互幫互助,羣裡還有不錯的視訊學習教學和PDF電子書!
'''
name='lhf'
age=19
msg='i am %s my hobby is %d' % (name,age) #age可以用%d or %s,但用%s程式可讀性差
print(msg)
執行結果:
i am lhf my hobby is 19
5、%d 只能接收數位型別,不能接收列表
'''
遇到問題沒人解答?小編建立了一個Python學習交流QQ羣:778463939
尋找有志同道合的小夥伴,互幫互助,羣裡還有不錯的視訊學習教學和PDF電子書!
'''
# %d 只能按收數位型別
msg='i am %s my hobby is %d' % ('lhf',1)
print(msg)
執行結果:
i am lhf my hobby is 1
6、%d 不能接收列表型別,會報錯
msg='i am %s my hobby is %d' % ('lhf',[1,2])
print(msg)
執行結果:
Traceback (most recent call last):
File "D:/python/day5/format_type.py", line 14, in <module>
msg='i am %s my hobby is %d' % ('lhf',[1,2])
TypeError: %d format: a number is required, not list
7、列印浮點數
tpl = "percent %.2f" % 99.976234444444444444
print(tpl)
執行結果:
percent 99.98
8、列印百分比
tpl = 'percent %.2f %%' % 99.976234444444444444
print(tpl)
執行結果:
percent 99.98 %
9、左邊列印空格
msg='i am %(name)+60s my hobby is alex' %{'name':'lhf'}
print(msg)
執行結果:
i am lhf my hobby is alex
10、列印空格加顏色(黃色)
msg='i am \033[43;1m%(name)+60s\033[0m my hobby is alex' %{'name':'lhf'}
print(msg)
執行結果:
i am lhf my hobby is alex
11、不用格式化的方式
print('root','x','0','0',sep=':')
print('root'+':'+'x'+':'+'0','0')
執行結果:
root:x:0:0
root:x:0 0
[[fill]align][sign][#][0][width][,][.precision][type]
<,內容左對齊
>,內容右對齊(預設)
=,內容右對齊,將符號放置在填充字元的左側,且只對數位型別有效。 即使:符號+填充物+數位
^,內容居中
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位)
常用格式化:
tpl = "i am {}, age {}, {}".format("seven", 18, 'alex')
#必須一一對應,否則會報錯
tpl = "i am {}, age {}, {}".format(*["seven", 18, 'alex'])
tpl = "i am {0}, age {1}, really {0}".format("seven", 18)
tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18])
tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)
tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})
** 代表傳字典
tpl = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33])
tpl = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1)
s 代表字串 d 代表整數
tpl = "i am {:s}, age {:d}".format(*["seven", 18])
* 代表列表
tpl = "i am {name:s}, age {age:d}".format(name="seven", age=18)
tpl = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18})
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
2進位制 8進位制 10進位制 x與X: 16進位制 %:百分比
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15)
tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15)
範例:
ps1:
tpl = "i am {0}, age {1}, really {2}".format("seven", '18','alex')
print(tpl)
執行結果:
i am seven, age 18, really alex
ps2:
tpl = "i am {2}, age {1}, really {0}".format("seven", '18','alex')
print(tpl)
執行結果:
i am alex, age 18, really seven
ps3: 傳字典的方式
tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)
print(tpl)
執行結果:
i am seven, age 18, really seven
ps4: ** 傳字典的方式 (跟ps3的方式是一樣的)
tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})
print(tpl)
執行結果:
i am seven, age 18, really seven
ps5: * 代表:列表
tpl = "i am {:s}, age {:d}".format(*["seven", 18])
print(tpl)
執行結果:
i am seven, age 18
ps6:
tpl = "i am {:s}, age {:d}".format("seven", 18) #["seven", 18]
print(tpl)
執行結果:
i am seven, age 18
ps7:
l=["seven", 18]
tpl = "i am {:s}, age {:d}".format('seven',18)
print(tpl)
執行結果:
i am seven, age 18
ps8: 2進位制 8進位制 10進位制 x與X: 16進位制 %:百分比
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%},{}".format(15, 15, 15, 15, 15, 15.87623, 2)
print(tpl)
執行結果:
numbers: 1111,17,15,f,F, 1587.623000%,2