Question: Python 7.6.9: Part 1, Remove All From String Write a function called remove_all_from_string that takes two strings, and returns a copy of the first string

Python

7.6.9: Part 1, Remove All From String

Write a function called remove_all_from_string that takes two strings, and returns a copy of the first string with all instances of the second string removed. You can assume that the second string is only one letter, like "a".

Test your function on the strings "hello" and "l". Print the result, which should be:

heo

You must use:

  • A function definition with parameters.
  • A while loop.
  • The find method.
  • Slicing and the + operator.
  • A return statement.

This is the code I've done so far and I'm getting an error on line 7.

string1 = ("hello") string2 = ("l") def remove_all_from_string(): while True: findstring2 = string1.find(string2) return (str(string1) + str(string1[findstring2]) print remove_all_from_string()

7.6.10: Part 2, Remove All From String

Write a function called remove_all_from_string that takes two strings, and returns a copy of the first string with all instances of the second string removed. This time, the second string may be any length, including 0.

Test your function on the strings "bananas" and "na". Print the result, which should be:

bas

You must use:

  • A function definition with parameters.
  • A while loop.
  • The find method.
  • The len function.
  • Slicing and the + operator.
  • A return statement.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!