Manipulating Strings - 5th Test Case failing | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Manipulating Strings - 5th Test Case failing

Question - 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. My Code (text part is predefined) - text = "Amy has 128 dollars, while David has 54 dollars. Amy is going to the zoo. David watches soccer." import re x = input() y = input() print(len(re.findall(x, text))) print(re.sub(x, y, text))

12th Jul 2021, 11:01 AM
Yash Sawant
6 Answers
+ 7
Yash Sawant , i can not figure out what the issue with your code is, as the test cases that fails is hidden. i would encourage you to use string method count() and string method replace(). this should work happy coding and good success!
12th Jul 2021, 1:57 PM
Lothar
Lothar - avatar
+ 5
Yash Sawant , there might be an issue when input for strings are both empty, or when string_x is empty and string_y has some characters, or the other way around. ? is your task description complete? can you name the tutorial and the lesson number? happy coding and good success.
12th Jul 2021, 11:22 AM
Lothar
Lothar - avatar
+ 2
text = input() old = input() new = input() print(text.count(old)) new_text = text.replace(old,new) print(new_text)
12th Jul 2021, 2:49 PM
Shadoff
Shadoff - avatar
+ 1
Lothar, Python Data Structures course and Working with Strings topic. The lesson number is 5.2 Editing Guide. My task description is complete, but I was thinking of other ways to solve it and then stumbled upon this problem.
12th Jul 2021, 12:14 PM
Yash Sawant
0
find = input() replace = input() print(text.count(find)) print(text.replace(find, replace))
20th Nov 2021, 12:43 PM
Stefan Bartl
Stefan Bartl - avatar
0
find = input() replace = input() print(text.count(find)) print(text.replace(find, replace))
17th Apr 2022, 6:52 AM
Максетов ТоребеKK