Python replace方法并不改变原字符串

直接给出结论: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)
posted @ 2019-01-31 20:28  MarkKobs  阅读(3627)  评论(0编辑  收藏  举报