+ 1

Manipulating Strings

Hey guys I am stuck in one of the project questions of data structures. Somehow one of the five tests cases fails. Please assist. The Q is as follows: You are making a text editor and need to implement find/replace functionality. The code declares a text string. You need to take two string inputs, which represent the substring to find and what to replace it with in the text. Your program needs to output the number of replacements made, along with the new text. For example, for the given text "I weigh 80 kilograms. He weighs 65 kilograms.": Sample Input kilograms kg Sample Output 2 I weigh 80 kg. He weighs 65 kg. The program replaced 2 occurrences of the word kilograms with kg. ****************** And my code is: text = "Amy has 128 dollars, while David has 54 dollars. Amy is going to the zoo. David watches soccer." a= input() b= input() x=text.replace(a,b) print(x.count(b)) print (x)

10th Jul 2021, 8:49 AM
Nur Hilal
4 Respostas
+ 7
Nur Hilal This should work: a = input() b = input() print(text.count(a)) x = text.replace(a, b) print(x) # Hope this helps
10th Jul 2021, 8:55 AM
Calvin Thomas
Calvin Thomas - avatar
0
It works, thanks a bunch
10th Jul 2021, 8:57 AM
Nur Hilal
0
text = "Amy has 128 dollars, while David has 54 dollars. Amy is going to the zoo. David watches soccer." x=str(input()) y=str(input()) print(text.count(x)) print(text.replace(x,y))
15th Sep 2021, 6:34 AM
Sri Virat Goka
Sri Virat Goka - avatar