Reverse Simplest Code


This is the simplest code for reversing a string text.
Though it is very simple, i don't understand the logic behind it, could someone help explain it please?
The main line of code that needs explanation is: result = i + result
Thanx in advance...

def reverse(text):
    result = ""
    for i in text:
        result = i + result
    return result


Pencil and paper are the best tools, still, for working out fundamental algorithms.

Given an iterable, a string, we course through the characters from left to right.

"some string"

"s" + ""
"o" + "s"
"m" + "os"
"e" + "mos"

See where this is going?


Ah now i get it, thank you very much.. :hushed: :relaxed: like a stack, the element that was put first is at the bottom & the last one is at the top..


really best code i found but cant understand somehow