Beginner in python here:
for l in 'helloworld':
if l == 'w':
continue
lnew = l
lnew = ''.join(lnew).replace('o', '').split()
print "The letter is", lnew
I am trying to remove the letter 'o' after letter 'w' from 'helloworld'. I understand that continue statement returns the control to the beginning of the while loop. But how do I make sure that it skips 'o' after the letter 'w' when it runs through.
I could have done l == 'wo' but that would defeat the purpose of learning.
Instead I tried to make a new list, where it would replace the letter 'o' with ' ' (blank) and then split the list but it replaces both 'o' as I get the following:
The letter is ['h']
The letter is ['e']
The letter is ['l']
The letter is ['l']
The letter is []
The letter is []
The letter is ['r']
The letter is ['l']
The letter is ['d']
What should I do so that only the letter 'o' after letter 'w' is removed after continue statement skips over the letter 'w'. (The answer should look like this)
The letter is h
The letter is e
The letter is l
The letter is l
The letter is o
The letter is r
The letter is l
The letter is d
I hope I understand what you meant. I changed the code a bit:
output:
So what I did was iterate over the string, and check if the current char is 'o' if yes - i'm using a boolean flag to check if it's the first one.
If the current char isn't 'o', just append it to the new_string