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

posted @ 2017-12-28 14:34  lzp的bky  阅读(16562)  评论(0编辑  收藏  举报