Python格式化輸出的具體實現(xiàn)
“%”的使用
| 格式符 | 描述 |
|---|---|
| %s | 字符串 (采用str()的顯示) |
| %r | 字符串 (采用repr()的顯示) |
| %c | 單個字符及其ASCII碼 |
| %u | 整數(shù)(無符號) |
| %b | 二進制整數(shù) |
| %o | 八進制數(shù)(無符號) |
| %d | 十進制整數(shù) |
| %i | 十進制整數(shù) |
| %x | 十六進制數(shù)(無符號) |
| %X | 十六進制數(shù)大寫(無符號) |
| %e | 指數(shù) (基底寫為e),用科學計數(shù)法格式化浮點數(shù) |
| %E | 指數(shù) (基底寫為E),作用同%e |
| %f | 浮點數(shù),可指定小數(shù)點后的精度 |
| %g | %f和%e的簡寫,指數(shù)(e)或浮點數(shù) (根據(jù)顯示長度) |
| %G | %F和%E的簡寫,指數(shù)(E)或浮點數(shù) (根據(jù)顯示長度) |
| %p | 用十六進制數(shù)格式化變量的地址 |
| %% | 轉義,字符"%" |
字符串輸出(%s)
- %10s——右對齊,占位符10位
- %-10s——左對齊,占位符10位
- %.2s——截取2位字符串
- %10.2s——10位占位符,截取兩位字符串
# 字符串輸出
print('%s' % 'hello world') # 結果:hello world
# 右對齊,取20位,不夠則補位
print('%20s' % 'hello world') # 結果: hello world
# 左對齊,取20位,不夠則補位
print('%-20s' % 'hello world') # 結果:hello world
# 取2位
print('%.2s' % 'hello world') # 結果:he
# 右對齊,占位符10位,取2位
print('%10.2s' % 'hello world') # 結果: he
# 左對齊,占位符10位,取2位
print('%-10.2s' % 'hello world') # 結果:he
浮點數(shù)輸出(%f)
%f ——保留小數(shù)點后面六位有效數(shù)字
%.3f,保留3位小數(shù)位
%e ——保留小數(shù)點后面六位有效數(shù)字,指數(shù)形式輸出
%.3e,保留3位小數(shù)位,使用科學計數(shù)法
%g ——在保證六位有效數(shù)字的前提下,使用小數(shù)方式,否則使用科學計數(shù)法
%.3g,保留3位有效數(shù)字,使用小數(shù)或科學計數(shù)法
# 默認保留6位小數(shù)
print('%f' % 1.11) # 1.110000
# 取1位小數(shù)
print('%.1f' % 1.11) # 結果:1.1
# 默認6位小數(shù),用科學計數(shù)法
print('%e' % 1.11) # 結果:1.110000e+00
# 取3位小數(shù),用科學計數(shù)法
print('%.3e' % 1.11) # 結果:1.110e+00
# 默認6位有效數(shù)字
print('%g' % 1111.1111) # 結果:1111.11
# 取7位有效數(shù)字
print('%.7g' % 1111.1111) # 結果:1111.111
# 取2位有效數(shù)字,自動轉換為科學計數(shù)法
print('%.2g' % 1111.1111) # 結果:1.1e+03
format的使用
位置匹配
① 不帶參數(shù),即{}
② 帶數(shù)字參數(shù),可調換順序,即{1}、{2}
③ 帶關鍵字,即{a}、{to}
# 不帶參數(shù)
print('{} {}'.format('hello','world')) # 結果:hello world
# 帶數(shù)字參數(shù)
print('{0} {1}'.format('hello','world')) # 結果:hello world
# 參數(shù)順序倒亂
print('{0} {1} {0}'.format('hello','world')) # 結果:hello world hello
# 帶關鍵字參數(shù)
print('{a} {tom} {a}'.format(tom='hello',a='world')) # 結果:world hello world
# 通過索引
coord = (3, 5)
print('X: {0[0]}; Y: {0[1]}'.format(coord)) # 結果:'X: 3; Y: 5'
# 通過key鍵參數(shù)
a = {'a': 'test_a', 'b': 'test_b'}
print('X: {0[a]}; Y: {0[b]}'.format(a)) # 結果:'X: test_a; Y: test_b'
格式轉換
| 符號 | 描述 |
|---|---|
| 'b' | 二進制。將數(shù)字以2為基數(shù)進行輸出 |
| 'c' | 字符。在打印之前將整數(shù)轉換成對應的Unicode字符串 |
| 'd' | 十進制整數(shù)。將數(shù)字以10為基數(shù)進行輸出 |
| 'o' | 八進制。將數(shù)字以8為基數(shù)進行輸出 |
| 'x' | 十六進制。將數(shù)字以16為基數(shù)進行輸出,9以上的位數(shù)用小寫字母 |
| 'e' | 冪符號。用科學計數(shù)法打印數(shù)字。用'e'表示冪 |
| 'g' | 一般格式。將數(shù)值以fixed-point格式輸出。當數(shù)值特別大的時候,用冪形式打印 |
| 'n' | 數(shù)字。當值為整數(shù)時和'd'相同,值為浮點數(shù)時和'g'相同。不同的是它會根據(jù)區(qū)域設置插入數(shù)字分隔符 |
| '%' | 百分數(shù)。將數(shù)值乘以100然后以fixed-point('f')格式打印,值后面會有一個百分號 |
print('{0:b}'.format(3)) # 結果:11
print('{:c}'.format(20)) # 結果:?
print('{:d}'.format(20)) # 結果:20
print('{:o}'.format(20)) # 結果:24
print('{:x}'.format(20)) # 結果:14
print('{:e}'.format(20)) # 結果:2.000000e+01
print('{:g}'.format(20.1)) # 結果:20.1
print('{:f}'.format(20)) # 結果:20.000000
print('{:n}'.format(20)) # 結果:20
print('{:%}'.format(20)) # 結果:2000.000000%
高階用法
進制轉換
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))
# 在前面加“#”,則帶進制前綴
# 輸出:int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010
左中右對齊及位數(shù)補全㈠ 對齊
| 符號 | 描述 |
|---|---|
| < | 左對齊(默認) |
| > | 右對齊 |
| ^ | 居中對齊 |
| = | 在小數(shù)點后進行補齊(只用于數(shù)字) |
㈡ 取位數(shù) “{:4s}”、"{:.2f}"等
# 默認左對齊
print('{} and {}'.format('hello','world')) # 結果:hello and world
# 取10位左對齊,取10位右對齊
print('{:10s} and {:>10s}'.format('hello','world')) # 結果:hello and world
# 取10位中間對齊
print('{:^10s} and {:^10s}'.format('hello','world')) # 結果: hello and world
# 取2位小數(shù)
print('{} is {:.2f}'.format(1.123,1.123)) # 結果:1.123 is 1.12
# 取2位小數(shù),右對齊,取10位
print('{0} is {0:>10.2f}'.format(1.123)) # 結果:1.123 is 1.12
# 左對齊
print('{:<30}'.format('left aligned')) # 結果:'left aligned '
# 右對齊
print('{:>30}'.format('right aligned')) # 結果:' right aligned'
# 中間對齊
print('{:^30}'.format('centered')) # 結果:' centered '
# 使用“*”填充
print('{:*^30}'.format('centered')) # 結果:'***********centered***********'
# 還有“=”只能應用于數(shù)字,這種方法可用“>”代替
print('{:0=30}'.format(11)) # '000000000000000000000000000011'
正負符號顯示正負符號顯示 %+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ù)時顯示-,與'{:f}; {:f}'一致
print('{:-f}; {:-f}'.format(3.14, -3.14)) # '3.140000; -3.140000'
百分數(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以達到格式化的目的,在{}里加入對象,此為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到此這篇關于Python格式化輸出的具體實現(xiàn)的文章就介紹到這了,更多相關Python格式化輸出內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python獲取時光網(wǎng)電影數(shù)據(jù)的實例代碼
這篇文章主要介紹了Python獲取時光網(wǎng)電影數(shù)據(jù),基本原理是先通過requests庫,通過時光網(wǎng)自帶的電影數(shù)據(jù)API接口,獲取到指定的電影數(shù)據(jù),本文結合示例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-09-09
python環(huán)境下OPenCV處理視頻流局部區(qū)域像素值
這篇文章主要為大家介紹了python環(huán)境下OPenCV處理視頻流局部區(qū)域像素值的實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2021-11-11
使用Python操作MySQL數(shù)據(jù)庫的教程詳解
在這篇文章中,主要為大家詳細介紹如何在Python中使用pymysql模塊來操作MySQL數(shù)據(jù)庫,文中的示例代碼簡介易懂,需要的小伙伴可以跟隨小編一起學習一下2023-07-07

