Python格式化輸出的具體實(shí)現(xiàn)
“%”的使用
格式符 | 描述 |
---|---|
%s | 字符串 (采用str()的顯示) |
%r | 字符串 (采用repr()的顯示) |
%c | 單個(gè)字符及其ASCII碼 |
%u | 整數(shù)(無(wú)符號(hào)) |
%b | 二進(jìn)制整數(shù) |
%o | 八進(jìn)制數(shù)(無(wú)符號(hào)) |
%d | 十進(jìn)制整數(shù) |
%i | 十進(jìn)制整數(shù) |
%x | 十六進(jìn)制數(shù)(無(wú)符號(hào)) |
%X | 十六進(jìn)制數(shù)大寫(xiě)(無(wú)符號(hào)) |
%e | 指數(shù) (基底寫(xiě)為e),用科學(xué)計(jì)數(shù)法格式化浮點(diǎn)數(shù) |
%E | 指數(shù) (基底寫(xiě)為E),作用同%e |
%f | 浮點(diǎn)數(shù),可指定小數(shù)點(diǎn)后的精度 |
%g | %f和%e的簡(jiǎn)寫(xiě),指數(shù)(e)或浮點(diǎn)數(shù) (根據(jù)顯示長(zhǎng)度) |
%G | %F和%E的簡(jiǎn)寫(xiě),指數(shù)(E)或浮點(diǎn)數(shù) (根據(jù)顯示長(zhǎng)度) |
%p | 用十六進(jìn)制數(shù)格式化變量的地址 |
%% | 轉(zhuǎn)義,字符"%" |
字符串輸出(%s)
- %10s——右對(duì)齊,占位符10位
- %-10s——左對(duì)齊,占位符10位
- %.2s——截取2位字符串
- %10.2s——10位占位符,截取兩位字符串
# 字符串輸出 print('%s' % 'hello world') # 結(jié)果:hello world # 右對(duì)齊,取20位,不夠則補(bǔ)位 print('%20s' % 'hello world') # 結(jié)果: hello world # 左對(duì)齊,取20位,不夠則補(bǔ)位 print('%-20s' % 'hello world') # 結(jié)果:hello world # 取2位 print('%.2s' % 'hello world') # 結(jié)果:he # 右對(duì)齊,占位符10位,取2位 print('%10.2s' % 'hello world') # 結(jié)果: he # 左對(duì)齊,占位符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ù)字,自動(dòng)轉(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
# 通過(guò)索引 coord = (3, 5) print('X: {0[0]}; Y: {0[1]}'.format(coord)) # 結(jié)果:'X: 3; Y: 5' # 通過(guò)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)換
符號(hào) | 描述 |
---|---|
'b' | 二進(jìn)制。將數(shù)字以2為基數(shù)進(jìn)行輸出 |
'c' | 字符。在打印之前將整數(shù)轉(zhuǎn)換成對(duì)應(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ù)用小寫(xiě)字母 |
'e' | 冪符號(hào)。用科學(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'相同。不同的是它會(huì)根據(jù)區(qū)域設(shè)置插入數(shù)字分隔符 |
'%' | 百分?jǐn)?shù)。將數(shù)值乘以100然后以fixed-point('f')格式打印,值后面會(huì)有一個(gè)百分號(hào) |
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
左中右對(duì)齊及位數(shù)補(bǔ)全㈠ 對(duì)齊
符號(hào) | 描述 |
---|---|
< | 左對(duì)齊(默認(rèn)) |
> | 右對(duì)齊 |
^ | 居中對(duì)齊 |
= | 在小數(shù)點(diǎn)后進(jìn)行補(bǔ)齊(只用于數(shù)字) |
㈡ 取位數(shù) “{:4s}”、"{:.2f}"等
# 默認(rèn)左對(duì)齊 print('{} and {}'.format('hello','world')) # 結(jié)果:hello and world # 取10位左對(duì)齊,取10位右對(duì)齊 print('{:10s} and {:>10s}'.format('hello','world')) # 結(jié)果:hello and world # 取10位中間對(duì)齊 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ù),右對(duì)齊,取10位 print('{0} is {0:>10.2f}'.format(1.123)) # 結(jié)果:1.123 is 1.12 # 左對(duì)齊 print('{:<30}'.format('left aligned')) # 結(jié)果:'left aligned ' # 右對(duì)齊 print('{:>30}'.format('right aligned')) # 結(jié)果:' right aligned' # 中間對(duì)齊 print('{:^30}'.format('centered')) # 結(jié)果:' centered ' # 使用“*”填充 print('{:*^30}'.format('centered')) # 結(jié)果:'***********centered***********' # 還有“=”只能應(yīng)用于數(shù)字,這種方法可用“>”代替 print('{:0=30}'.format(11)) # '000000000000000000000000000011'
正負(fù)符號(hào)顯示正負(fù)符號(hào)顯示 %+f, %-f, 和 % f的用法
# 總是顯示符號(hào) 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%'
逗號(hào)作為千位分隔符,金額表示
print('{:,}'.format(1234567890)) # '1,234,567,890'
format變形用法
在字符串前加f以達(dá)到格式化的目的,在{}里加入對(duì)象,此為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
到此這篇關(guān)于Python格式化輸出的具體實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Python格式化輸出內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用sleep間隔進(jìn)行python反爬蟲(chóng)的實(shí)例講解
在本篇文章里小編給大家整理了一篇關(guān)于用sleep間隔進(jìn)行python反爬蟲(chóng)的實(shí)例講解內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2020-11-11Python獲取時(shí)光網(wǎng)電影數(shù)據(jù)的實(shí)例代碼
這篇文章主要介紹了Python獲取時(shí)光網(wǎng)電影數(shù)據(jù),基本原理是先通過(guò)requests庫(kù),通過(guò)時(shí)光網(wǎng)自帶的電影數(shù)據(jù)API接口,獲取到指定的電影數(shù)據(jù),本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09Python 判斷 有向圖 是否有環(huán)的實(shí)例講解
下面小編就為大家分享一篇Python 判斷 有向圖 是否有環(huán)的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02python3實(shí)現(xiàn)點(diǎn)餐系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python3實(shí)現(xiàn)點(diǎn)餐系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01python環(huán)境下OPenCV處理視頻流局部區(qū)域像素值
這篇文章主要為大家介紹了python環(huán)境下OPenCV處理視頻流局部區(qū)域像素值的實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11使用Python操作MySQL數(shù)據(jù)庫(kù)的教程詳解
在這篇文章中,主要為大家詳細(xì)介紹如何在Python中使用pymysql模塊來(lái)操作MySQL數(shù)據(jù)庫(kù),文中的示例代碼簡(jiǎn)介易懂,需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-07-07python使用epoll實(shí)現(xiàn)服務(wù)端的方法
今天小編就為大家分享一篇python使用epoll實(shí)現(xiàn)服務(wù)端的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10Python+Pytorch實(shí)戰(zhàn)之彩色圖片識(shí)別
這篇文章主要為大家詳細(xì)介紹了如何利用Python+Pytorch實(shí)現(xiàn)彩色圖片識(shí)別功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-09-09