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

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

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

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

1. 字符串替換

將需要替換的內(nèi)容使用格式化符替代,后續(xù)補(bǔ)上替換內(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. 字符串命名格式化符替換

使用命名格式化符,這樣,對(duì)于多個(gè)相同變量的引用,在后續(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ù)的語(yǔ)法方式:

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

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

3.模版方法替換

使用string中的Template方法;

通過(guò)關(guān)鍵字傳遞參數(shù):

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

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

from string import Template

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 一级毛片一片毛 | 欧美中日韩在线 | 国产成人精品视频2021 | 日韩中文在线播放 | 欧美 综合 社区 国产 | 亚洲黄色在线播放 | 91福利精品老师国产自产在线 | 亚洲国产最新在线一区二区 | 欧美日韩一区二区综合 | 欧美特级毛片a够爽天狼影院 | 欧美视频一区二区 | 精品美女在线观看 | 成人毛片18岁女人毛片免费看 | 国产亚洲精品久久久久久久久激情 | 久久精品视频免费在线观看 | 色婷婷五 | 亚洲精品一线观看 | 妖精永久免费观看 | 国产精品久久久久久福利漫画 | 一级特黄色毛片免费看 | 蛇女欲潮三级在线看 | 日韩在线视频www色 日韩在线视频一区 | 亚洲 欧美 中文字幕 | 日韩黄色录像 | 日韩欧美在线播放 | 亚洲精品美女视频 | 免费 欧美 自拍 在线观看 | 一级毛片一级毛片免费毛片 | 一级毛片不收费 | 香蕉爱爱视频 | 免费国产成人高清视频网站 | 永久免费不卡在线观看黄网站 | 国产精品日产三级在线观看 | 国产v欧美v日韩在线观看 | 黄色网址网站 | 美女久久久久 | 国产精品自拍亚洲 | 日韩日韩日韩日韩日韩 | 制服丝袜 自拍偷拍 | 国产亚洲欧美在线播放网站 | 一级视频在线免费观看 |