I’m writing a function to reverse a string. I have this:
def RevSen(lst):
lst = lst.split()
rl = []
for x in lst:
word = lst.pop()
rl.append(word)
reversedSnt = " ".join(rl)
return reversedSnt
mySnt = "we are all friends"
test = RevSen(mySnt)
print(test)
But when I test it, I got “friends all”. I cannot make it access the first two words.
What’s wrong? Is my loop incorrect?
I replaced the string with a list of integer. I also tried using insert and remove to replace “append”, but got the same results. I also tried having 3 words in the string. Same thing.
1