亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Python基于template實現(xiàn)字符串替換

瀏覽:14日期:2022-07-04 09:58:52

下面介紹使用python字符串替換的方法;

1. 字符串替換

將需要替換的內(nèi)容使用格式化符替代,后續(xù)補上替換內(nèi)容;

template = 'hello %s , your website is %s ' % ('大CC','http://blog.me115.com')print(template)

也可使用format函數(shù)完成:

template = 'hello {0} , your website is {1} '.format('大CC','http://blog.me115.com')print(template)

注:該方法適用于變量少的單行字符串替換;

2. 字符串命名格式化符替換

使用命名格式化符,這樣,對于多個相同變量的引用,在后續(xù)替換只用申明一次即可;

template = 'hello %(name)s ,your name is %(name), your website is %(message)s' %{'name':'大CC','message':'http://blog.me115.com'}print(template)

使用format函數(shù)的語法方式:

template = 'hello {name} , your name is {name}, your website is {message} '.format(name='大CC',message='http://blog.me115.com')print(template)

注:適用相同變量較多的單行字符串替換;

3.模版方法替換

使用string中的Template方法;

通過關鍵字傳遞參數(shù):

from string import TemplatetempTemplate = Template('Hello $name ,your website is $message')print(tempTemplate.substitute(name=’大CC’,message=’http://blog.me115.com’))

通過字典傳遞參數(shù):

from string import Template

tempTemplate = Template('There $a and $b')d={’a’:’apple’,’b’:’banbana’}print(tempTemplate.substitute(d))

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 国产黄色录像 | 亚洲国产成人99精品激情在线 | 久久综合九色综合精品 | 久久综合九色综合桃花 | 国产1区2区| 三亚美女一级毛片 | 97在线观免费视频观看 | 国产精品高清视亚洲一区二区 | 综合在线视频精品专区 | 日韩特黄特色大片免费视频 | 婷婷色天使在线视频观看 | 黄色毛片免费在线观看 | 午夜社区 | 久久狠狠干 | 888xxxx免费视频 | 国产高清尿小便嘘嘘视频 | 亚洲网站在线免费观看 | 亚洲午夜一区二区三区 | 中文字幕日韩哦哦哦 | 在线看日本a毛片 | 最新91视频 | 宅女深夜福利视频在线 | 久久精品视频国产 | 在线播放国产精品 | 久久久综合网 | 麻豆视频成人 | 国产免费久久精品99 | 国产91av视频在线观看 | 国产精品视频久久久久 | 成年网址网站在线观看 | 国产在线观看91精品亚瑟 | 国产成人免费手机在线观看视频 | 日韩电影中文字幕在线网站 | 久久国产偷 | 色婷婷狠狠 | 尤物视频网在线观看 | 中文字幕亚洲无线码 | 91原创视频 | 6080伦理久久亚洲精品 | 一级特黄特黄的大片免费 | 色y视频|