最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2020年11月27日 11:25:13   作者:-零  
這篇文章主要介紹了Python基于template實(shí)現(xiàn)字符串替換,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

下面介紹使用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ān)鍵字傳遞參數(shù):

from string import Template
tempTemplate = 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))

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

相關(guān)文章

最新評(píng)論

娄烦县| 札达县| 柏乡县| 仁怀市| 新巴尔虎左旗| 东莞市| 特克斯县| 天柱县| 扶风县| 家居| 通道| 武威市| 淮南市| 平潭县| 陕西省| 平远县| 密山市| 凤冈县| 泰州市| 类乌齐县| 德清县| 姚安县| 兴化市| 金阳县| 宁海县| 兴文县| 哈巴河县| 闽侯县| 芦山县| 翁牛特旗| 冷水江市| 泾源县| 遂昌县| 罗山县| 广州市| 金阳县| 黑水县| 迁西县| 河东区| 黔东| 无极县|