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

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

Python基于template實現字符串替換

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

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

1. 字符串替換

將需要替換的內容使用格式化符替代,后續補上替換內容;

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

也可使用format函數完成:

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

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

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

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

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

使用format函數的語法方式:

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

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

3.模版方法替換

使用string中的Template方法;

通過關鍵字傳遞參數:

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

通過字典傳遞參數:

from string import Template

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

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

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 午夜伦4480yy妇女久久久 | 成人18网站 | 手机在线观看a | 高清国产露脸捆绑01经典 | 2021国产精品自在拍在线播放 | 欧美操人 | 狠狠色狠狠色综合日日32 | 泰国情欲片寂寞的寡妇在线观看 | 国产成人精品本亚洲 | 蘑菇午夜三级 | 国产精品自拍亚洲 | 久久精品视频2 | 亚洲天堂日韩在线 | 日韩国产欧美一区二区三区在线 | 国产精品久久久久久麻豆一区 | 91色老99久久九九爱精品 | 日本久久一区二区 | 亚洲欧美另类自拍 | 久久婷婷影院 | 一级欧美激情毛片 | 男人天堂男人天堂 | 真实国产乱子伦高清 | 91久久国产口精品久久久久 | 亚洲精品手机在线观看 | 亚洲专区一 | 免费一级毛片不卡在线播放 | 欧美成人视屏 | 99久久一区二区精品 | 91免费公开视频 | 久草在线视频精品 | 国产第三区 | 激情一区二区三区成人 | 青青青青爽视频在线播放 | 中国人免费观看高清在线观看二区 | 午夜欧美成人香蕉剧场 | 欧美大片一区二区三区 | 91久久国产口精品久久久久 | 呦视频在线一区二区三区 | 欧美视频在线一区二区三区 | 日韩激情中文字幕一区二区 | 免费看日韩欧美一级毛片 |