Manipulating Strings | Sololearn: Learn to code for FREE!

+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)

7/10/2021 8:49:39 AM

Nur Hilal

4 Answers

New Answer

+6

Nur Hilal This should work: a = input() b = input() print(text.count(a)) x = text.replace(a, b) print(x) # Hope this helps

+2

https://code.sololearn.com/cIWi2Xkri2Wt/?ref=app

0

It works, thanks a bunch

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))