亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python格式化輸出的具體實(shí)現(xiàn)

 更新時(shí)間:2022年06月17日 10:46:15   作者:Zzmi  
本文主要介紹了Python格式化輸出的具體實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

“%”的使用

格式符描述
%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)文章

最新評(píng)論