Python replace方法並不改變原字串

MarkKobs發表於2019-01-31

直接給出結論:replace方法不會改變原字串。

temp_str = `this is a test`
print(temp_str.replace(`is`,`IS`)
print(temp_str)
thIS IS a test
this is a test

如果是需要對原字串進行替換,可以這樣寫,重新賦值

a=a.replace(oldstr,newstr)
print(a)

相關文章