python replace函數不起作用的坑


字符串的替換函數replace有一個坑,

a = "123456"

a.replace("6","7")

print a

結果還是"123456"

看看replace函數的介紹,

Return a copy of string S with all occurrences of substring
old replaced by new. If the optional argument count is
given, only the first count occurrences are replaced.

替換之后原來是返回一個新的copy,正確的做法是:

a = "123456"

b = a.replace("6","7")

print b


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM