Python strings have a built-in replace () method that can replace all occurrences of a word...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Python strings have a built-in replace () method that can replace all occurrences of a word in a string with another word. But today, we're going to see how to implement this ourselves. Note that we are only replacing full words, not substrings or parts of words. Write a small, but complete Python 3 program called Lab7B.py that does the following: a. Create a user-defined function called replace_word (sentence, target, replace) that accepts three string arguments, a sentence consisting of one or more words separated by a space, a target word that will be replaced, and finally, a replacement word for the target word. This function returns a copy of the sentence string with every instance of the target word replaced by the replacement word. For example, replace_word ("I am happy to meet you!", "happy", "angry") should return the string "I am angry to meet you!". You do not have to worry about handling punctuation correctly. Instead of using the replace () method, you will iterate over the words in the sentence string. First, use the split () method to split your string into a list of words. Then iterate over the length of the list. This will allow you to get the index in the list so that you can replace the target word with the replacement word. You will return a copy of the string with all occurrences of the target word replaced by the replacement word. Use the join () method to reconstruct the list back into a sentence and return that string. b. In the main part of the program, prompt the user for and read in a sentence (that may include several words separated by spaces) as a string. c. Prompt for and read in a target word as a string to remove from that sentence. d. Prompt for and read in a replacement word as a string to replace the target word in that sentence. e. Call the replace_word () function, passing in the sentence, the target word, and the replacement word input by the user, assigning the result to a new string variable that will contain the copy of the sentence with all occurrences of the word removed. f. If no word or words were replaced from the original sentence, then write an appropriate message that no changes were made. Otherwise, write an appropriate message that includes the new sentence. g. Now, let's incorporate the remove_letter () function from the Lab7A.py file. To do this, we must add the import Lab 7A line to the top of our file. Doing this will cause the main part of Lab7A.py to be executed before our code in Lab7B.py, so we must also modify the code in our Lab7A.py file to only execute if the name attribute is equal to '_____main_ '. Note that adding this if statement to our Lab7A.py file will still allow that lab file to work as expected for that lab component: lif name main sentence =input('Enter a sentence: ') letter = = input('Enter letter to remove from sentence: ') : h. Now, prompt for and read in a single-character string to remove from our newly modified sentence. i. Call the remove_letter () function from the Lab7A.py file, passing in the already modified sentence and the single-character letter just now input by the user, assigning the result to a new string variable that will contain the copy of the sentence with all occurrences of the letter removed. Note that you will have to prefix Lab 7A to the remove_letter () function to get it to work. j. Finally, write an appropriate message indicating all occurrences of the letter were removed, including the single-character and the sentence in the message. For example, the output might look like this (input shown in bold): $ python3 Lab7B.py Enter a sentence: It will be sunny this week! Enter a word in sentence to replace: sunny Enter a replacement word: rainy Here is the new sentence: It will be rainy this week! Enter letter to remove from sentence: y New sentence with letter y removed: It will be rain this week! $ python3 Lab7B.py Enter a sentence: It will be sunny this week! Enter a word in sentence to replace: nothing Enter a replacement word: something No changes were made to the original sentence Enter letter to remove from sentence: i New sentence with letter i removed: It wll be sunny ths week! Python strings have a built-in replace () method that can replace all occurrences of a word in a string with another word. But today, we're going to see how to implement this ourselves. Note that we are only replacing full words, not substrings or parts of words. Write a small, but complete Python 3 program called Lab7B.py that does the following: a. Create a user-defined function called replace_word (sentence, target, replace) that accepts three string arguments, a sentence consisting of one or more words separated by a space, a target word that will be replaced, and finally, a replacement word for the target word. This function returns a copy of the sentence string with every instance of the target word replaced by the replacement word. For example, replace_word ("I am happy to meet you!", "happy", "angry") should return the string "I am angry to meet you!". You do not have to worry about handling punctuation correctly. Instead of using the replace () method, you will iterate over the words in the sentence string. First, use the split () method to split your string into a list of words. Then iterate over the length of the list. This will allow you to get the index in the list so that you can replace the target word with the replacement word. You will return a copy of the string with all occurrences of the target word replaced by the replacement word. Use the join () method to reconstruct the list back into a sentence and return that string. b. In the main part of the program, prompt the user for and read in a sentence (that may include several words separated by spaces) as a string. c. Prompt for and read in a target word as a string to remove from that sentence. d. Prompt for and read in a replacement word as a string to replace the target word in that sentence. e. Call the replace_word () function, passing in the sentence, the target word, and the replacement word input by the user, assigning the result to a new string variable that will contain the copy of the sentence with all occurrences of the word removed. f. If no word or words were replaced from the original sentence, then write an appropriate message that no changes were made. Otherwise, write an appropriate message that includes the new sentence. g. Now, let's incorporate the remove_letter () function from the Lab7A.py file. To do this, we must add the import Lab 7A line to the top of our file. Doing this will cause the main part of Lab7A.py to be executed before our code in Lab7B.py, so we must also modify the code in our Lab7A.py file to only execute if the name attribute is equal to '_____main_ '. Note that adding this if statement to our Lab7A.py file will still allow that lab file to work as expected for that lab component: lif name main sentence =input('Enter a sentence: ') letter = = input('Enter letter to remove from sentence: ') : h. Now, prompt for and read in a single-character string to remove from our newly modified sentence. i. Call the remove_letter () function from the Lab7A.py file, passing in the already modified sentence and the single-character letter just now input by the user, assigning the result to a new string variable that will contain the copy of the sentence with all occurrences of the letter removed. Note that you will have to prefix Lab 7A to the remove_letter () function to get it to work. j. Finally, write an appropriate message indicating all occurrences of the letter were removed, including the single-character and the sentence in the message. For example, the output might look like this (input shown in bold): $ python3 Lab7B.py Enter a sentence: It will be sunny this week! Enter a word in sentence to replace: sunny Enter a replacement word: rainy Here is the new sentence: It will be rainy this week! Enter letter to remove from sentence: y New sentence with letter y removed: It will be rain this week! $ python3 Lab7B.py Enter a sentence: It will be sunny this week! Enter a word in sentence to replace: nothing Enter a replacement word: something No changes were made to the original sentence Enter letter to remove from sentence: i New sentence with letter i removed: It wll be sunny ths week!
Expert Answer:
Related Book For
Business Communication Essentials a skill based approach
ISBN: 978-0132971324
6th edition
Authors: Courtland L. Bovee, John V. Thill
Posted Date:
Students also viewed these programming questions
-
Write a program that replaces all occurrences of a word with a new word in all the files under a directory, recursively. Pass the parameters from the command line as follows: java Exercise18_31...
-
Write a program that finds all occurrences of a word in all the files under a directory, recursively. Pass the parameters from the command line as follows: java Exercise18_30 dirName word
-
Write a function that will return a full name string when passed a first name string, a middle name or initial string, and a last name string. If the middle initial is blank, there should be only one...
-
Use the Chain Rule to calculate the partial derivatives. Express the answer in terms of the independent variables. dh t -; h(x, y) = X x = tt, y = tt " y
-
For what kind of business would soliciting private venture capital be a logical financial strategy for growth? Why?
-
Evaluate 3x-9 when x = -2. The value is
-
When you walk through still air at a rate of \(1 \mathrm{~m} / \mathrm{s}\), would you expect the character of the airflow around you to be most like that depicted in Fig. 9.6a, b, or c? Explain....
-
Effective January 1, 1970, Chrysler Corporation adopted the FIFO method for inventories previously valued by the LIFO method. The 1970 annual report stated, This . makes the financial statements with...
-
Define ADR. Provide an example of when you would want to use ADR and another example of when you would not want to use ADR and use a different method. Identify that second method and explain why you...
-
Funtime, Inc., manufactures video game machines. Market saturation and technological innovations caused pricing pressures that resulted in declining profits. To stem the slide in profits until new...
-
The total charge induced in a conducting loop when it is moved in a magnetic field depends on (a) the rate of change of magnetic flux (b) initial magnetic flux only (c) the total change in magnetic...
-
ssume Southwest Airlines has the following information available about total miles traveled and operating costs for one of its aircrafts: Year Miles Traveled Operating Costs Year 1 123,500 $228,700...
-
3. The manufacturing of Styrofoam coffee cups causes damage to a local river when the manufacturing plant produces more than 500 units in a period. To discourage the plant from producing more than...
-
An exoplanet orbits, a star at an orbital distance of 1 . 2 9 1 . 2 9 astronomical units. The period of the orbit is 2 . 0 5 2 . 0 5 years. Calculate the mass of the star in SI unit.
-
Accounts Income Statement Revenues Cost of goods sold Allen Co. Bradford Co. Debit (485,000) (190,000) 160,000 70,000 Depreciation expense 130,000 52,000 (E) Amortization expense (E) Equity in...
-
2 Riscko debtor schedule as at 31 May 20XX correction task You are Joan Jacks and you are an Accountant who works for Riskco, a reputable risk management consultancy company. You are checking the...
-
A major urban center is planning to issue a $100 million, 20-year, semiannual-interest-paying municipal bond for the construction of a stadium. -The interest rate is 5.875%, based on the economic and...
-
In a nonmagnetic medium, E = 50 cos (10 9 t 8x) a y + 40 sin (10 9 t 8x) a z V/m find the dielectric constant r and the corresponding H.
-
The time and energy required for careful revision can often benefit you or your company directly. For example, reader-friendly product descriptions will increase the probability that website visitors...
-
The coffee shop across the street from your tiny apartment is your haven away from home great beverages, healthysnacks, an atmosphere that is convivial but not so lively that you can't focus on...
-
Your boss wants to send a brief email message to welcome employees recently transferred to your department from your Hong Kong branch. They all speak English to some degree, but your boss asks you to...
-
The following information has been extracted from the recently published statements of company D, as set out in Figure 7.11. Figure 7.11 Financial statements for company D as at 30 April The ratios...
-
Under IFRS, an asset is something: A. Owned. B. Used. C. Owned and controlled. D. Controlled.
-
Which of the following costs related to a building is NOT usually treated as an expense but is added to the asset? A. Repairs. B. Redecoration. C. Fitting new safety equipment. D. Cleaning.
Study smarter with the SolutionInn App