描述
Python replace() 方法把字符串中的 old(舊字符串) 替換成 new(新字符串),如果指定第三個參數max,則替換不超過 max 次。
語法
replace()方法語法:
str.replace(old, new[, max])
實例1
#!/usr/bin/python
str = "this is string example....wow!!! this is really string";
print str.replace("is", "was");
print str.replace("is", "was", 3);
以上實例輸出結果如下:
thwas was string example....wow!!! thwas was really string thwas was string example....wow!!! thwas is really string
實例2
strip是trim掉字符串兩邊的空格。
lstrip, trim掉左邊的空格
rstrip, trim掉右邊的空格
wdata = str(wrlist[i].replace('[', '')).replace(']', '') # 去掉字符的中括號 wdata = wdata.replace(''''','').replace(''''', '') # 去掉字符的引號
theString = ' https://mp.csdn.net/postlist ' print (theString.strip())