
string類是python中最常用的文本處理工具,在python的標(biāo)準(zhǔn)庫中,有大量的工具,可以幫助我們完成高級文本處理。

import string
s= 'The quick brown fox jumped over the lazy dog.'print s
print string.capwords(s)運(yùn)行結(jié)果如下:
The quick brown fox jumped over the lazy dog.
The Quick Brown Fox Jumped Over The Lazy Dog.相當(dāng)于先調(diào)用split(),這會將結(jié)果列表中的各個(gè)單詞的首字母大寫,然后再調(diào)用join()合并結(jié)果。
import string
leet= string.maketrans('abegiloprstz', '463611092572')
s= 'The quick brown fox jumped over the lazy dog.'print s
print s.translate(leet)運(yùn)行結(jié)果如下:
The quick brown fox jumped over the lazy dog.
Th3 qu1ck 620wn f0x jum93d 0v32 7h3 142y d06.import string
values= {'var' : 'foo'}
t= string.Template("""
Variable : $var
Escape : $$
Variable in text : ${var}iable
""")
print 'TEMPLATE:', t.substitute(values)
s= """
Variable : %(var)s
Escape : %%
Variable in text : %(var)siable
"""print 'INTERPOLATION:', s % values運(yùn)行結(jié)果
TEMPLATE:
Variable : foo
Escape : $
Variable in text : fooiable
INTERPOLATION:
Variable : foo
Escape : %
Variable in text : fooiable
網(wǎng)頁名稱:pythonstring文本常量和模版-創(chuàng)新互聯(lián)
瀏覽路徑:http://www.chinadenli.net/article16/dgejdg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、虛擬主機(jī)、電子商務(wù)、商城網(wǎng)站、靜態(tài)網(wǎng)站、網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容