Reverse Simplest Code


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/1/1?curriculum_id=4f89dab3d788890003000096
<In what way does your code behave incorrectly? Include ALL error messages.>
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…

<What do you expect to happen instead?>

```python

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

<do not remove the three backticks above>

#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