Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

You must login to ask a question.

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Quizzma Latest Articles

7.6.9 Part 1, Remove All From String CodeHS Answers

The most common answer is:

update the function to return word with all instances of letter removed!
word = “Egg”
letter = “g”
def remove_all_from_string(word, letter):
while True:
index = word.find(letter)
if index == -1:
return word
word = word[:index] + word[index+1:]
return word
print (remove_all_from_string(word, letter))

This function remove_all_from_string is intended to remove all instances of letter from word. The loop and string slicing logic you’ve used are on the right track but can be simplified.

Additionally, the return word statement within the loop will never be reached because of the preceding return statement when index == -1. Let’s streamline the function and correct it:

word = "Egg"
letter = "g"

def remove_all_from_string(word, letter):
    # A more straightforward way to remove all instances of letter from word
    return word.replace(letter, "")

print(remove_all_from_string(word, letter))

This version uses the str.replace(old, new) method to replace all instances of letter with an empty string, effectively removing them. This approach is more concise and directly accomplishes the goal without manually searching for indices and slicing the string.

Was this helpful?




Quizzma Team

Quizzma Team

The Quizzma Team is a collective of experienced educators, subject matter experts, and content developers dedicated to providing accurate and high-quality educational resources. With a diverse range of expertise across various subjects, the team collaboratively reviews, creates, and publishes content to aid in learning and self-assessment.
Each piece of content undergoes a rigorous review process to ensure accuracy, relevance, and clarity. The Quizzma Team is committed to fostering a conducive learning environment for individuals and continually strives to provide reliable and valuable educational resources on a wide array of topics. Through collaborative effort and a shared passion for education, the Quizzma Team aims to contribute positively to the broader learning community.

Related Posts

Leave a comment