Question: The following function should return a copy of the input string with all digits removed. However it contains a logic error. What's wrong, and how

The following function should return a copy of the input string with all digits removed. However it contains a logic error. What's wrong, and how would you fix it?
def remove_digits(s):
new_s =""
for letter in s:
if letter.isdigit():
new_s = new_s + letter
return new_s
Group of answer choices
The code adds characters if they are digits, rather than if they're not digits. The condition should be reversed.
def remove_digits(s):
new_s =""
for letter in s:
if not letter.isdigit():
new_s = new_s + letter
return new_s
new_s should be reassigned to the matching letter each time the loop finds a digit.
def remove_digits(s):
new_s =""
for letter in s:
if letter.isdigit():
new_s = letter
return new_s
The string concatenation for new_s is backwards. It should be
def remove_digits(s):
new_s =""
for letter in s:
if letter.isdigit():
new_s = letter + new_s
return new_s
There should not be a new_s, instead modify s directly.
def remove_digits(s):
for letter in s:
if letter.isdigit():
s = s + letter
return s

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!