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