Run
# Given string x = "apple apple apple apple apple" # Limit on number of replacements n = 3 # Old and new strings old = 'apple' new = 'fig' # Replace first n occurrences result = x.replace(old, new, n) # Print the result string print(result)
Output