Run
# Input string x = "Hello, World! Hello, Universe!" # Substring to be replaced old_substring = "Hello" # Replacement string new_substring = "Hi" # Find the last occurrence of the old_substring last_occurrence_index = x.rfind(old_substring) # Replace result_string = x[:last_occurrence_index] + new_substring + x[last_occurrence_index + len(old_substring):] # Print the result print(f"Original : {x}") print(f"Result : {result_string}")
Output