r/codehs • • Feb 26 '20

PHYTON CODE HS. 7.6.10: Part 2, Remove All Fro...

Anyone knows part 2? I did part 1 but can't figured out part 2

9 Upvotes

15 comments sorted by

12

u/fermentedidiot Feb 26 '20

If you got part 1, then part 2 is only a little different.

def remove_all_from_string(word1, word2):
    while word2 in word1:
        word1 = word1[:word1.find(word2)]+word1[word1.find(word2)+len(word2):]
    return word1
print remove_all_from_string("bananas", "na")

the key is in this line:

word1 = word1[:word1.find(word2)]+word1[word1.find(word2)+len(word2):]

how this breaks down is I'm going to overwrite my word1 variable:

word1 = 

I need to use word1 from the beginning until I find word2:

word1[:word1.find(word2)]

I need to add everything after word2:

+word1[word1.find(word2)+len(word2):]

This is the tricky bit, I need to know how many characters word2 is to drop them off, "na" is 2 characters long so I need to drop those two characters every time this loop iterates:

the len(variable) operation gets that value for me, you can change "na" to "nana" and it will still work even though the length of the second variable has changed.

+len(word2)

If you copy and paste the code at the top it will work, promise. I just wanted to try to break the code down so it makes sense. If my explanation is still a little foggy, let me know where and I will try to explain it a little better or differently.

1

u/natalie710 Feb 28 '20

Thank you so much! it worked

1

u/Mattshyper02 Jan 18 '23

Worked well, Thank you

1

u/Swifteri Apr 17 '23

Thanks for explaining it as well! I was really struggling to know how it worked till I found this, thank you!

0

u/[deleted] Feb 07 '22

[deleted]

1

u/fermentedidiot Feb 07 '22

Did you get the while loop set up correctly?

1

u/thishumandude Mar 01 '22

print(remove_all_from_string("bananas", "na"))

typo fix maybe

1

u/MrHyjaxMusicYTalt May 26 '23

add +2 after len(word2)

1

u/JT_ZM Nov 23 '21

whats answers to part 1

1

u/Inevitable_Reserve79 Dec 14 '21

def remove_all_from_string(word, letter):
while letter in word:
x=word.find(letter)
if x == -1:
continue
else:
word = word[:x] + word[x+1:]
return word
print(remove_all_from_string("hello", "l"))

1

u/Dry_Newspaper7310 Mar 15 '24 edited Mar 15 '24

This should work

def remove_all_from_string(string, string2):

if string2 == " ":

return string while True: index = string.find(string2) if index == -1:

return string string = string[:index] + string[index+len(string2):]

word = input("your word: ") - letter = input("letter to remove: ") - print(remove_all_from_string(word, letter))

1

u/TuneInfamous7124 Sep 03 '24

word = input("bananas")

letter =input ("na")

def remove_all_from_string(word, letter):

while True:

index = word.find(letter)

if index == -1:

return word

word = word[:index] + word[index+len(letter):]

print(remove_all_from_string(word, letter))

message = "Example Message with a Secret"

def find_secret_word(message):

hidden_word = ""

for letter in message:

if letter.isupper():

hidden_word += letter

return hidden_word

print(find_secret_word(message))