Reverse Simplest Code


#1



https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/1/1?curriculum_id=4f89dab3d788890003000096

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


#2

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?


#3

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..


#4

really best code i found but cant understand somehow