色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

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

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

瀏覽:20日期: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方法;

通過關(guān)鍵字傳遞參數(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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 香蕉久久久久 | 国产精品99久久久久久人 | 国产偷国产偷亚洲高清午夜 | 99视频免费观看 | 国产精品亚洲第一区柳州莫青 | 日本高清一本二本三本如色坊 | 久久99亚洲精品久久频 | 免费一级特黄欧美大片勹久久网 | 亚洲成年人专区 | 国产精品成人在线播放 | 国产香蕉在线视频一级毛片 | 欧美yyy | 日韩在线视频不卡一区二区三区 | 美国一级免费 | 亚洲欧美综合一区二区三区四区 | 国产农村乱 | 美女张开腿让我 | 成人做爰www| 欧美一区二区三区gg高清影视 | 欧美日本一道高清二区三区 | 一本久道久久综合婷婷五 | 午夜毛片视频高清不卡免费 | 禁止18周岁进入免费网站观看 | 国产黄色在线播放 | 在线免费观看国产 | 亚洲国产精品日韩在线观看 | 国产一级做性视频 | 毛片在线免费观看网站 | 欧美日韩精品一区二区三区不卡 | 久久怡红院国产精品 | 2019天天操天天干天天透 | 国产精品精品 | 国产在线精品成人一区二区三区 | 成人国产第一区在线观看 | 日日a.v拍夜夜添久久免费 | 一区二区三区在线免费看 | 亚洲天堂男人天堂 | 免费看一级欧美激情毛片 | 国产系列 视频二区 | 成人免费视频网站 | 一本久道久久综合婷婷五 |