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)


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM