“%”的使用
格式符 | 描述 |
---|---|
%s | 字符串 (采用str()的顯示) |
%r | 字符串 (采用repr()的顯示) |
%c | 單個(gè)字符及其ASCII碼 |
%u | 整數(shù)(無符號) |
%b | 二進(jìn)制整數(shù) |
%o | 八進(jìn)制數(shù)(無符號) |
%d | 十進(jìn)制整數(shù) |
%i | 十進(jìn)制整數(shù) |
%x | 十六進(jìn)制數(shù)(無符號) |
%X | 十六進(jìn)制數(shù)大寫(無符號) |
%e | 指數(shù) (基底寫為e),用科學(xué)計(jì)數(shù)法格式化浮點(diǎn)數(shù) |
%E | 指數(shù) (基底寫為E)掖疮,作用同%e |
%f | 浮點(diǎn)數(shù)初茶,可指定小數(shù)點(diǎn)后的精度 |
%g | %f和%e的簡寫颗祝,指數(shù)(e)或浮點(diǎn)數(shù) (根據(jù)顯示長度) |
%G | %F和%E的簡寫浊闪,指數(shù)(E)或浮點(diǎn)數(shù) (根據(jù)顯示長度) |
%p | 用十六進(jìn)制數(shù)格式化變量的地址 |
%% | 轉(zhuǎn)義,字符"%" |
字符串輸出(%s)
%10s——右對齊,占位符10位
%-10s——左對齊螺戳,占位符10位
%.2s——截取2位字符串
%10.2s——10位占位符搁宾,截取兩位字符串
# 字符串輸出
print('%s' % 'hello world') # 結(jié)果:hello world
# 右對齊,取20位倔幼,不夠則補(bǔ)位
print('%20s' % 'hello world') # 結(jié)果: hello world
# 左對齊盖腿,取20位,不夠則補(bǔ)位
print('%-20s' % 'hello world') # 結(jié)果:hello world
# 取2位
print('%.2s' % 'hello world') # 結(jié)果:he
# 右對齊损同,占位符10位翩腐,取2位
print('%10.2s' % 'hello world') # 結(jié)果: he
# 左對齊,占位符10位膏燃,取2位
print('%-10.2s' % 'hello world') # 結(jié)果:he
浮點(diǎn)數(shù)輸出(%f)
%f ——保留小數(shù)點(diǎn)后面六位有效數(shù)字
%.3f茂卦,保留3位小數(shù)位
%e ——保留小數(shù)點(diǎn)后面六位有效數(shù)字,指數(shù)形式輸出
%.3e组哩,保留3位小數(shù)位等龙,使用科學(xué)計(jì)數(shù)法
%g ——在保證六位有效數(shù)字的前提下,使用小數(shù)方式伶贰,否則使用科學(xué)計(jì)數(shù)法
%.3g蛛砰,保留3位有效數(shù)字,使用小數(shù)或科學(xué)計(jì)數(shù)法
# 默認(rèn)保留6位小數(shù)
print('%f' % 1.11) # 1.110000
# 取1位小數(shù)
print('%.1f' % 1.11) # 結(jié)果:1.1
# 默認(rèn)6位小數(shù)黍衙,用科學(xué)計(jì)數(shù)法
print('%e' % 1.11) # 結(jié)果:1.110000e+00
# 取3位小數(shù)泥畅,用科學(xué)計(jì)數(shù)法
print('%.3e' % 1.11) # 結(jié)果:1.110e+00
# 默認(rèn)6位有效數(shù)字
print('%g' % 1111.1111) # 結(jié)果:1111.11
# 取7位有效數(shù)字
print('%.7g' % 1111.1111) # 結(jié)果:1111.111
# 取2位有效數(shù)字,自動轉(zhuǎn)換為科學(xué)計(jì)數(shù)法
print('%.2g' % 1111.1111) # 結(jié)果:1.1e+03
format的使用
位置匹配
① 不帶參數(shù)琅翻,即{}
② 帶數(shù)字參數(shù)位仁,可調(diào)換順序,即{1}望迎、{2}
③ 帶關(guān)鍵字障癌,即{a}、{to}
# 不帶參數(shù)
print('{} {}'.format('hello','world')) # 結(jié)果:hello world
# 帶數(shù)字參數(shù)
print('{0} {1}'.format('hello','world')) # 結(jié)果:hello world
# 參數(shù)順序倒亂
print('{0} {1} {0}'.format('hello','world')) # 結(jié)果:hello world hello
# 帶關(guān)鍵字參數(shù)
print('{a} {tom} {a}'.format(tom='hello',a='world')) # 結(jié)果:world hello world
# 通過索引
coord = (3, 5)
print('X: {0[0]}; Y: {0[1]}'.format(coord)) # 結(jié)果:'X: 3; Y: 5'
# 通過key鍵參數(shù)
a = {'a': 'test_a', 'b': 'test_b'}
print('X: {0[a]}; Y: {0[b]}'.format(a)) # 結(jié)果:'X: test_a; Y: test_b'
格式轉(zhuǎn)換
符號 | 描述 |
---|---|
'b' | 二進(jìn)制辩尊。將數(shù)字以2為基數(shù)進(jìn)行輸出 |
'c' | 字符涛浙。在打印之前將整數(shù)轉(zhuǎn)換成對應(yīng)的Unicode字符串 |
'd' | 十進(jìn)制整數(shù)。將數(shù)字以10為基數(shù)進(jìn)行輸出 |
'o' | 八進(jìn)制。將數(shù)字以8為基數(shù)進(jìn)行輸出 |
'x' | 十六進(jìn)制轿亮。將數(shù)字以16為基數(shù)進(jìn)行輸出疮薇,9以上的位數(shù)用小寫字母 |
'e' | 冪符號。用科學(xué)計(jì)數(shù)法打印數(shù)字我注。用'e'表示冪 |
'g' | 一般格式按咒。將數(shù)值以fixed-point格式輸出。當(dāng)數(shù)值特別大的時(shí)候但骨,用冪形式打印 |
'n' | 數(shù)字励七。當(dāng)值為整數(shù)時(shí)和'd'相同,值為浮點(diǎn)數(shù)時(shí)和'g'相同奔缠。不同的是它會根據(jù)區(qū)域設(shè)置插入數(shù)字分隔符 |
'%' | 百分?jǐn)?shù)掠抬。將數(shù)值乘以100然后以fixed-point('f')格式打印,值后面會有一個(gè)百分號 |
print('{0:b}'.format(3)) # 結(jié)果:11
print('{:c}'.format(20)) # 結(jié)果:?
print('{:d}'.format(20)) # 結(jié)果:20
print('{:o}'.format(20)) # 結(jié)果:24
print('{:x}'.format(20)) # 結(jié)果:14
print('{:e}'.format(20)) # 結(jié)果:2.000000e+01
print('{:g}'.format(20.1)) # 結(jié)果:20.1
print('{:f}'.format(20)) # 結(jié)果:20.000000
print('{:n}'.format(20)) # 結(jié)果:20
print('{:%}'.format(20)) # 結(jié)果:2000.000000%
高階用法
進(jìn)制轉(zhuǎn)換
print("int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}".format(42))
# 輸出:int: 42; hex: 2a; oct: 52; bin: 101010
print("int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(42))
# 在前面加“#”校哎,則帶進(jìn)制前綴
# 輸出:int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010
左中右對齊及位數(shù)補(bǔ)全
㈠ 對齊
符號 | 描述 |
---|---|
< | 左對齊(默認(rèn)) |
> | 右對齊 |
^ | 居中對齊 |
= | 在小數(shù)點(diǎn)后進(jìn)行補(bǔ)齊(只用于數(shù)字) |
㈡ 取位數(shù) “{:4s}”两波、"{:.2f}"等
# 默認(rèn)左對齊
print('{} and {}'.format('hello','world')) # 結(jié)果:hello and world
# 取10位左對齊,取10位右對齊
print('{:10s} and {:>10s}'.format('hello','world')) # 結(jié)果:hello and world
# 取10位中間對齊
print('{:^10s} and {:^10s}'.format('hello','world')) # 結(jié)果: hello and world
# 取2位小數(shù)
print('{} is {:.2f}'.format(1.123,1.123)) # 結(jié)果:1.123 is 1.12
# 取2位小數(shù)闷哆,右對齊腰奋,取10位
print('{0} is {0:>10.2f}'.format(1.123)) # 結(jié)果:1.123 is 1.12
# 左對齊
print('{:<30}'.format('left aligned')) # 結(jié)果:'left aligned '
# 右對齊
print('{:>30}'.format('right aligned')) # 結(jié)果:' right aligned'
# 中間對齊
print('{:^30}'.format('centered')) # 結(jié)果:' centered '
# 使用“*”填充
print('{:*^30}'.format('centered')) # 結(jié)果:'***********centered***********'
# 還有“=”只能應(yīng)用于數(shù)字,這種方法可用“>”代替
print('{:0=30}'.format(11)) # '000000000000000000000000000011'
正負(fù)符號顯示
正負(fù)符號顯示 %+f, %-f, 和 % f的用法
# 總是顯示符號
print('{:+f}; {:+f}'.format(3.14, -3.14)) # '+3.140000; -3.140000'
# 若是+數(shù)抱怔,則在前面留空格
print('{: f}; {: f}'.format(3.14, -3.14)) # ' 3.140000; -3.140000'
# -數(shù)時(shí)顯示-劣坊,與'{:f}; {:f}'一致
print('{:-f}; {:-f}'.format(3.14, -3.14)) # '3.140000; -3.140000'
百分?jǐn)?shù)%
points = 19
total = 22
print('Correct answers: {:.2%}'.format(points/total)) # 'Correct answers: 86.36%'
逗號作為千位分隔符,金額表示
print('{:,}'.format(1234567890)) # '1,234,567,890'
format變形用法
在字符串前加f以達(dá)到格式化的目的野蝇,在{}里加入對象讼稚,此為format的另一種形式
name = 'jack'
age = 18
sex = 'man'
job = "IT"
salary = 9999.99
print(f'my name is {name.capitalize()}.') # my name is Jack.
print(f'I am {age:*^10} years old.') # I am ****18**** years old.
print(f'I am a {sex}') # I am a man
print(f'My salary is {salary:10.3f}') # My salary is 9999.990