字符串的替换函数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

相关文章:

  • 2021-08-31
  • 2022-02-05
  • 2022-12-23
  • 2022-12-23
  • 2021-05-18
  • 2021-09-16
  • 2021-12-05
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案