Partition the string by the fragment and replace it only in the tail:
a, b, c = string.partition(fragment) output = a + b + c.replace(fragment, '')