Question: Write a function clean(txt) to split the text string txt into a list of words, and then concatenate all words into a new text string
Write a function clean(txt) to split the text string txt into a list of words, and then concatenate all words into a new text string inserting one empty space ' ' between them. At the end, return the result as a new text string. Make sure that the result does not end with a trailing space - you can do that using the method rstrip(). Use the docstring """Removes unwanted spaces and special characters""". Do not change the main program. Expected output: "If debugging is the process of removing software bugs, then programming must be the process of putting them in." (Edsger W. Dijkstra) (Edsger W. Dijkstra (1930 - 2002) of Dutch origin was a famous computer programmer, science essayist, and early pioneer in computer science.)
# Main program (do not change): text = '"If debugging is the process of removing software bugs, then programming must be the process of putting them in." (Edsger W. Dijkstra) ' print(clean(text))
In your function, first insert the docstring. Then split the text string txt into a list of words L. Create a new empty text string result. Use a for loop to go through the list L, and in each cycle update result with the word plus one empty space ' ' (use the += operator for that). After the loop ends, result will have one trailing space at the end. Remove it using the method rstrip() before returning it.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
