Answer:
Here is the function replace_all:
def replace_all(target_string,find_string,replace_string):
result_string = replace_string.join(target_string.split(find_string))
return result_string
#if you want to see the working of this function then you can call this function using the following statements:
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 above program has a function called replace_all that accepts three arguments i.e target_string, a string in which to search ,find_string, a string to search for and replace_string, a string to use to replace every instance of the value of find_string. The function has the following statement:
replace_string.join(target_string.split(find_string))
split() method in the above statement is splits or breaks the target_string in to a list of strings based on find_string. Here find_string is passed to split() method as a parameter which works as a separator. For example above:
target_string = In case I don't see ya, good afternoon, good evening, and good night!
find_string = good
After using target_string.split(find_string) we get:
["In case I don't see ya, ", ' afternoon, ', ' evening, and ', ' night']
Next join() method is used to return a string concatenated with the elements of target_string.split(find_string). Here join takes the list ["In case I don't see ya, ", ' afternoon, ', ' evening, and ', ' night'] as parameter and joins elements of this list by 'bad'.
replace_string = bad
After using replace_string.join(target_string.split(find_string)) we get:
In case I don't see ya, bad afternoon, bad evening, and bad night!
The program and output is attached as a screenshot.