Thursday, 5 September 2013

Python: Iterate over string with while loop

Python: Iterate over string with while loop

I'm trying to delete all the characters at the end of a string following
the last occurrence of a '+'. So for instance, if my string is
'Mother+why+is+the+river+laughing' I want to reduce this to
'Mother+why+is+the+river'. I don't know what the string will be in advance
though.
I thought of iterating backwards over the string. Something like:
while letter in my_string[::-1] != '+':
my_string = my_string[:-1]
This won't work because letter in not pre defined.
Any thoughts?

No comments:

Post a Comment