Answer:
Here is the function called replace_all:
def replace_all(target_string,find_string,replace_string):
result_string = replace_string.join(target_string.split(find_string))
return result_string
#To see how this function operates, you can invoke it using the following code:
target_string = "In case I don't see ya, good afternoon, good evening, and good night!"
find_string = "good"
replace_string = "bad"
print(replace_all(target_string, find_string, replace_string)
Explanation:
The aforementioned program contains a function named replace_all that accepts three parameters: target_string, which is the string to search within; find_string, which is the string you are searching for; and replace_string, which is the string utilized to replace all occurrences of find_string. The function executes the following statement:
replace_string.join(target_string.split(find_string))
The split() method in this context divides target_string into a list according to find_string. Here, find_string is provided to the split() method as a parameter, acting as a separator. For example:
target_string = In case I don't see ya, good afternoon, good evening, and good night!
find_string = good
After executing target_string.split(find_string), we obtain:
["In case I don't see ya, ", ' afternoon, ', ' evening, and ', ' night']
The join() method then concatenates the elements of the list generated by target_string.split(find_string). Here, join takes that list as a parameter and connects its elements using 'bad'.
The resulting replace_string = badSubsequently, after using replace_string.join(target_string.split(find_string)), we generate:
In case I don't see ya, bad afternoon, bad evening, and bad night!
The code and output are attached as a screenshot.