Python實(shí)現(xiàn)字符串格式化輸出的方法詳解
本文實(shí)例講述了Python實(shí)現(xiàn)字符串格式化輸出的方法。分享給大家供大家參考,具體如下:
python屬于強(qiáng)類(lèi)型的語(yǔ)言,如果像java一樣操作字符串和數(shù)字的“+”時(shí),會(huì)出現(xiàn)TypeError。而python的格式化方法有多種,比如使用占位符,使用format,或者是自定義模版等等。這里介紹了其中的幾種方法
下面這個(gè)例子很好的說(shuō)明了python屬于強(qiáng)類(lèi)型語(yǔ)言:
print "abc" + 123 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: cannot concatenate 'str' and 'int' objects
所以,需要進(jìn)行轉(zhuǎn)換輸出。
常用占位符
| 符號(hào) | 意思 |
| %s | 字符串 |
| %d / %i | 十進(jìn)制整數(shù) |
| %u | 過(guò)時(shí)的十進(jìn)制使用方法 |
| %o | 八進(jìn)制整數(shù) |
| %x / %X | 十六進(jìn)制整數(shù) |
| %f / %F | 浮點(diǎn)數(shù) |
| %e / %E | 科學(xué)技術(shù)法 |
| %% | 輸出% |
使用方式一
直接使用占位符
print '%s+%d' % ('abc', 123) #abc+123
print '%o' % 10 #12 八進(jìn)制
為%d指定長(zhǎng)度,%05d,如果數(shù)字小于5位會(huì)在左邊補(bǔ)0,大于指定長(zhǎng)度時(shí)不受此影響
print '%s+%05d' % ('abc', 123) #abc+00123
print '%03x' % 10 #00a
print '%.3e' % 123456789 #1.235e+08 保留3位小數(shù)的科學(xué)技術(shù)法
使用方式二
使用字典
當(dāng)拼接有許多重復(fù)元素時(shí),使用這種方式比較好
使用方式三
使用format的方式。在2.6之后的版本支持。
print '{0}{1}{2}{3}'.format('a', 'b', 'c', 123) #abc123
print '{}, {}, {}'.format('a', 'b', 'c') #abc 2.7+ only
print '{2}, {1}, {0}'.format('a', 'b', 'c') #c, b, a
print '{2}, {1}, {0}'.format(*'abc') #c, b, a
print '{0}{1}{0}'.format('abra', 'cad') #abracadabra
通過(guò)參數(shù)名字格式化
print 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W') #Coordinates: 37.24N, -115.81W
coord = {'latitude': '37.24N', 'longitude': '-115.81W'}
print 'Coordinates: {latitude}, {longitude}'.format(**coord) #Coordinates: 37.24N, -115.81W
使用元組
coord = (3, 5)
print 'X: {0[0]}; Y: {0[1]}'.format(coord) #X: 3; Y: 5
進(jìn)制
# format also supports binary numbers
"int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}".format(42) #'int: 42; hex: 2a; oct: 52; bin: 101010'
3
# with 0x, 0o, or 0b as prefix:
"int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(42) #'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010'
為數(shù)字加點(diǎn)號(hào)
'{:,}'.format(1234567890) #'1,234,567,890'
百分比表示
'{:.2%}'.format(19.5 / 22) # '88.64%'
時(shí)間格式化
import datetime
today = datetime.datetime.today()
'{:%Y-%m-%d %H:%M:%S}'.format(d) #'2013-09-01 21:10:22'
'{:%Y-%m-%d}'.format(today) #'2013-09-01'
另外也可以使用strftime來(lái)格式化時(shí)間
使用方式四
自定義模版
from string import Template
s = Template('$sargs plus $aargs')
s.substitute(sargs = 'abc', aargs = 123) #'abc plus 123'
這里有substitue和safe_substitute兩種屬性
d = dict(sargs = 'abc') # s.substitute(d) # it's a KeyError s.safe_substitute(d) #'abc plus $aargs'
如果不使用safe_substitute,參數(shù)不全時(shí)會(huì)出現(xiàn)KeyError異常。
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》及《Python入門(mén)與進(jìn)階經(jīng)典教程》。
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python文檔字符串(函數(shù)使用說(shuō)明)使用詳解
這篇文章主要介紹了python文檔字符串(函數(shù)使用說(shuō)明)使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
Pandas index操作索引的實(shí)現(xiàn)
Pandas中的索引index用于選擇特定的行數(shù)和列數(shù),加快數(shù)據(jù)訪問(wèn)速度,本文就來(lái)介紹一下index操作索引,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
Pytorch結(jié)合PyG實(shí)現(xiàn)MLP過(guò)程詳解
這篇文章主要為大家介紹了Pytorch結(jié)合PyG實(shí)現(xiàn)MLP過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
python opencv實(shí)現(xiàn)圖像配準(zhǔn)與比較
這篇文章主要為大家詳細(xì)介紹了python opencv實(shí)現(xiàn)圖像配準(zhǔn)與比較,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-02-02
解讀python中的類(lèi)型提示(type hint)
這篇文章主要介紹了解讀python中的類(lèi)型提示(type hint),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
七個(gè)生態(tài)系統(tǒng)核心庫(kù)[python自學(xué)收藏]
無(wú)論你是想快速入手Python,還是想成為數(shù)據(jù)分析大神或者機(jī)器學(xué)習(xí)大佬,亦或者對(duì)Python代碼進(jìn)行優(yōu)化,本文的python庫(kù)都能為你提供一些幫助2021-08-08
python的mysql數(shù)據(jù)庫(kù)建立表與插入數(shù)據(jù)操作示例
這篇文章主要介紹了python的mysql數(shù)據(jù)庫(kù)建立表與插入數(shù)據(jù)操作,結(jié)合實(shí)例形式分析了python操作mysql數(shù)據(jù)庫(kù)建立表與插入數(shù)據(jù)相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-09-09
利用在Python中數(shù)值模擬研究氣體擴(kuò)散
在 Python 中,可以使用數(shù)值模擬來(lái)研究氣體擴(kuò)散。本文就來(lái)通過(guò)一些示例為大家講講具體的實(shí)現(xiàn)方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以學(xué)習(xí)一下2023-01-01

