-1

I know how to replace characters, but I would like to have the instance done once. My code replace's all the characters before.

string = "Forever9999"

string = string[:-4] + string[-4:].replace("9", "1")

Which in the end would be Forever1999, but I get Forever1111

Any help appreciated.

1 Answers1

0

You can pass another paramter to str.replace(old, new[, count]), which is the max count of occurrences you want to replace:

string.replace("9", "1", 1)
# 'Forever1999'
user2390182
  • 72,016
  • 6
  • 67
  • 89