「作者主頁(yè)」:士別三日wyx
「作者簡(jiǎn)介」:CSDN top100、阿里云博客專(zhuān)家、華為云享專(zhuān)家、網(wǎng)絡(luò)安全領(lǐng)域優(yōu)質(zhì)創(chuàng)作者
「推薦專(zhuān)欄」:小白零基礎(chǔ)《Python入門(mén)到精通》
replace() 可以「替換」字符串中的內(nèi)容
語(yǔ)法
string.replace( old, new, count )
參數(shù)
- old :(必選,字符串類(lèi)型)被替換的字符串
- new :(必選,字符串類(lèi)型)替換后的字符串
- 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)存地址沒(mé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í),無(wú)論負(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ì)打印出來(lái),但 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)?font color="blue"> replace() 返回的是一個(gè)字符串,所以我們可以對(duì)返回的結(jié)果再次replace(),比如下面這樣:
str1 = 'zhangsan lisi wangwu'
print(str1.replace('zhangsan', 'new').replace('lisi', 'new'))
輸出:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-567158.html
new new wangwu
有多個(gè)內(nèi)容需要替換時(shí),可以使用這種簡(jiǎn)化的方式。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-567158.html
到了這里,關(guān)于Python replace()函數(shù)使用詳解,Python替換字符串的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!