python中使用replace替換多個字符的方法:
1、使用replace方法替換多個相連的字符
#!/usr/bin/python
str="thisisstringexample....wow!!!thisisreallystring";
printstr.replace("is","was");
printstr.replace("is","was",3);
輸出結(jié)果如下:
thwaswasstringexample....wow!!!thwaswasreallystring
thwaswasstringexample....wow!!!thwasisreallystring
2、在replace()函數(shù)中使用正則替換多個字符
//定義一個數(shù)組
letstr=[[1],[2],[3],[4]];
//先轉(zhuǎn)換成JSON字符串
letstr2=JSON.stringify(str);
//在對該字符串進行字符替換,在這里我們將所有中括號都替換成空
console.log(str2.replace(/\[|]/g,''));
//這樣就完成了多個字符的替換
以上內(nèi)容為大家介紹了python如何replace(替換)多個字符?希望對大家有所幫助,如果想要了解更多Python相關(guān)知識,請關(guān)注IT培訓(xùn)機構(gòu):千鋒教育。