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
You can use string.rindex to find the right most o. There are other options too, look here: If we cannot find 'o' ValueError is raised and we will just pass making str2 same as str1
Output