Python替換字符串replace()函數(shù)使用方法詳解
replace() 可以「替換」字符串中的內(nèi)容
語法
string.replace( old, new, count )
參數(shù)
- old :(必選,字符串類型)被替換的字符串
- new :(必選,字符串類型)替換后的字符串
- count :(可選,整型)替換的次數(shù)
返回值
- 返回替換后的新字符串
實(shí)例:將字符串中的 “hello” 替換成 “world”
str1 = 'hello hello hello hello world' str2 = str1.replace('hello', 'world') print(str2)
輸出:
world world world world world
1、不改變?cè)址?/h2>
因?yàn)镻ython中的字符串是「不可變」的,所以 replace() 不會(huì)改變?cè)址膬?nèi)容,而是返回一個(gè)新的字符串。
我們分別打印替換前、后的兩個(gè)字符串「內(nèi)容」和「內(nèi)存地址」。
str1 = 'hello hello hello hello world' print(id(str1)) str2 = str1.replace('hello', 'world') print(str1, id(str1)) print(str2, id(str2))
輸出:
2834751121168
hello hello hello hello world 2834751121168
world world world world world 2834751121568
可以看到,原字符串的內(nèi)容和內(nèi)存地址沒有發(fā)生變化。
2、指定替換次數(shù)
「不指定」次數(shù),默認(rèn)替換「所有」匹配到的字符串
str1 = 'hello_1 hello_2 hello_3 hello_4' print(str1.replace('hello', 'world'))
輸出:
world_1 world_2 world_3 world_4
替換次數(shù)為「正數(shù)」時(shí),按照從左到右的順序替換,設(shè)置幾次就替換幾次
str1 = 'hello_1 hello_2 hello_3 hello_4' print(str1.replace('hello', 'world', 1)) print(str1.replace('hello', 'world', 3))
輸出:
world_1 hello_2 hello_3 hello_4
world_1 world_2 world_3 hello_4
替換次數(shù)為「負(fù)數(shù)」時(shí),無論負(fù)幾,都會(huì)替換所有匹配到的內(nèi)容
str1 = 'hello_1 hello_2 hello_3 hello_4' print(str1.replace('hello', 'world', -1)) print(str1.replace('hello', 'world', -3))
輸出:
world_1 world_2 world_3 world_4
world_1 world_2 world_3 world_4
指定的次數(shù)必須是「整型」,否則會(huì)報(bào)錯(cuò) TypeError: ‘str’ object cannot be interpreted as an integer
或者 TypeError: integer argument expected,
3、轉(zhuǎn)義符
字符串中的轉(zhuǎn)義符不會(huì)打印出來,但 replace() 可以替換這些轉(zhuǎn)義符,比如替換換行符\n
str1 = 'hello world\n' print(str1) print(str1.replace('\n', ' new'))
輸出:
hello world
hello world new
從結(jié)果可以看到,替換前會(huì)換行,替換后不會(huì)換行,因?yàn)檗D(zhuǎn)義符被替換掉了。
4、替換列表、元組、字典的元素
對(duì)「列表」中的元素使用 replace() ,可以使用下面這種方式
arr = ['hello', 'hello', 'hello'] print([string.replace('hello', 'world') for string in arr])
輸出:
['world', 'world', 'world']
這種方式本質(zhì)上是生成了一個(gè)「新數(shù)組」,我們可以看一下內(nèi)存地址
arr = ['hello', 'hello', 'hello'] print(id(arr)) print(id([string.replace('hello', 'world') for string in arr]))
輸出:
1658941612416
1658941612544
或者使用「循環(huán)」的方式替換列表中的元素,這種方式不會(huì)生成新數(shù)組,替換前、后的內(nèi)存地址是一樣的。
arr1 = ['hello', 'hello', 'hello'] print(arr1, id(arr1)) for a in range(len(arr1)): arr1[a] = arr1[a].replace('hello', 'world') print(arr1, id(arr1))
輸出:
['hello', 'hello', 'hello'] 1672076599552
['world', 'world', 'world'] 1672076599552
替換「元祖」中的元素,需要先轉(zhuǎn)成列表,再循環(huán)替換,替換完成再轉(zhuǎn)回元組,這種方式同樣會(huì)改變內(nèi)存地址。
tu = ('hello', 'hello', 'hello') print(id(tu)) arr1 = list(tu) for a in range(len(arr1)): arr1[a] = arr1[a].replace('hello', 'world') tu = tuple(arr1) print(tu, id(tu))
輸出:
2255689005696
('world', 'world', 'world') 2255689005824
替換「字典」的值,直接循環(huán)替換
dic = {'key1': 'zhangsan', 'key2': 'lisi'} for a in dic: dic[a] = dic[a].replace('zhangsan', 'new') print(dic)
輸出:
{'key1': 'new', 'key2': 'lisi'}
5、連續(xù)替換
因?yàn)?nbsp;replace() 返回的是一個(gè)字符串,所以我們可以對(duì)返回的結(jié)果再次replace(),比如下面這樣:
str1 = 'zhangsan lisi wangwu' print(str1.replace('zhangsan', 'new').replace('lisi', 'new'))
輸出:
new new wangwu
有多個(gè)內(nèi)容需要替換時(shí),可以使用這種簡(jiǎn)化的方式。
到此這篇關(guān)于Python替換字符串replace()函數(shù)使用方法詳解的文章就介紹到這了,更多相關(guān)Python replace()函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
appium+python自動(dòng)化配置(adk、jdk、node.js)
這篇文章主要介紹了appium+python自動(dòng)化配置(adk、jdk、node.js),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Pytest測(cè)試報(bào)告工具Allure用法介紹
這篇文章介紹了Pytest測(cè)試報(bào)告工具Allure的用法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07Python3enumrate和range對(duì)比及示例詳解
這篇文章主要介紹了Python3enumrate和range對(duì)比及示例詳解,在Python中,enumrate和range都常用于for循環(huán)中,enumrate函數(shù)用于同時(shí)循環(huán)列表和元素,而range()函數(shù)可以生成數(shù)值范圍變化的列表,而能夠用于for循環(huán)即都是可迭代的,需要的朋友可以參考下2019-07-07詳解Python數(shù)據(jù)可視化編程 - 詞云生成并保存(jieba+WordCloud)
這篇文章主要介紹了Python數(shù)據(jù)可視化編程 - 詞云生成并保存(jieba+WordCloud),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03正則表達(dá)式在Python中的應(yīng)用小結(jié)
正則表達(dá)式是一種強(qiáng)大的文本模式匹配工具,它可以幫助我們快速地檢索、替換或提取字符串中的特定模式,在本文中,我將通過一些示例代碼,詳細(xì)介紹正則表達(dá)式在Python中的應(yīng)用,感興趣的朋友一起看看吧2024-07-07使用Python內(nèi)置模塊與函數(shù)進(jìn)行不同進(jìn)制的數(shù)的轉(zhuǎn)換
這篇文章主要介紹了使用Python內(nèi)置模塊與函數(shù)進(jìn)行不同進(jìn)制的數(shù)的轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Python qqbot 實(shí)現(xiàn)qq機(jī)器人的示例代碼
這篇文章主要介紹了Python qqbot 實(shí)現(xiàn)qq機(jī)器人的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07