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