Python替換字符串replace()函數使用方法詳解
replace() 可以「替換」字符串中的內容
語法
string.replace( old, new, count )
參數
- old :(必選,字符串類型)被替換的字符串
- new :(必選,字符串類型)替換后的字符串
- count :(可選,整型)替換的次數
返回值
- 返回替換后的新字符串
實例:將字符串中的 “hello” 替換成 “world”
str1 = 'hello hello hello hello world' str2 = str1.replace('hello', 'world') print(str2)
輸出:
world world world world world
1、不改變原字符串
因為Python中的字符串是「不可變」的,所以 replace() 不會改變原字符串的內容,而是返回一個新的字符串。
我們分別打印替換前、后的兩個字符串「內容」和「內存地址」。
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
可以看到,原字符串的內容和內存地址沒有發(fā)生變化。
2、指定替換次數
「不指定」次數,默認替換「所有」匹配到的字符串
str1 = 'hello_1 hello_2 hello_3 hello_4' print(str1.replace('hello', 'world'))
輸出:
world_1 world_2 world_3 world_4
替換次數為「正數」時,按照從左到右的順序替換,設置幾次就替換幾次
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
替換次數為「負數」時,無論負幾,都會替換所有匹配到的內容
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
指定的次數必須是「整型」,否則會報錯 TypeError: ‘str’ object cannot be interpreted as an integer
或者 TypeError: integer argument expected,
3、轉義符
字符串中的轉義符不會打印出來,但 replace() 可以替換這些轉義符,比如替換換行符\n
str1 = 'hello world\n' print(str1) print(str1.replace('\n', ' new'))
輸出:
hello world
hello world new
從結果可以看到,替換前會換行,替換后不會換行,因為轉義符被替換掉了。
4、替換列表、元組、字典的元素
對「列表」中的元素使用 replace() ,可以使用下面這種方式
arr = ['hello', 'hello', 'hello'] print([string.replace('hello', 'world') for string in arr])
輸出:
['world', 'world', 'world']
這種方式本質上是生成了一個「新數組」,我們可以看一下內存地址
arr = ['hello', 'hello', 'hello'] print(id(arr)) print(id([string.replace('hello', 'world') for string in arr]))
輸出:
1658941612416
1658941612544
或者使用「循環(huán)」的方式替換列表中的元素,這種方式不會生成新數組,替換前、后的內存地址是一樣的。
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
替換「元祖」中的元素,需要先轉成列表,再循環(huán)替換,替換完成再轉回元組,這種方式同樣會改變內存地址。
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ù)替換
因為 replace() 返回的是一個字符串,所以我們可以對返回的結果再次replace(),比如下面這樣:
str1 = 'zhangsan lisi wangwu' print(str1.replace('zhangsan', 'new').replace('lisi', 'new'))
輸出:
new new wangwu
有多個內容需要替換時,可以使用這種簡化的方式。
到此這篇關于Python替換字符串replace()函數使用方法詳解的文章就介紹到這了,更多相關Python replace()函數內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
appium+python自動化配置(adk、jdk、node.js)
這篇文章主要介紹了appium+python自動化配置(adk、jdk、node.js),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11詳解Python數據可視化編程 - 詞云生成并保存(jieba+WordCloud)
這篇文章主要介紹了Python數據可視化編程 - 詞云生成并保存(jieba+WordCloud),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-03-03