7/15 Reverse - help me explain


Hello guys, i got some help with my friend to make this reverse exercise, someone could please explain me the lines?

**for i in range(len(text)): **
** reversed_text += text[len(text)-1-i]**

i really couldnt get it, so if anyone could explain to be, i`d be grateful,

def reverse(text):
    reversed_text = ""
    for i in range(len(text)):   
        reversed_text += text[len(text)-1-i]
        print reversed_text
    return reversed_text

print reverse("hello")


Hello, @moscrow!
The line of code:

reversed_text += text[len(text)-1-i]

Will simply get the element at that exact position, you get after doing that calculation, and will concatenate with the new string, which you named reversed_text.
Here's what happens, look at the value printed when I write:

print len(text)-1-i

It goes from the last position (N - 1) till 0.

If that's not clear, let me know and I'll find another way to explain!

Edit: I forgot to mention about range(len(text)), my apologies.
The range() function returns a list of numbers so doing that into a for loop is pretty much saying:
"Iterate over this list of N numbers"

The length of the text is five (5), it will bring back a list containing [0,1,2,3,4] and you'll iterate through it.


The range method takes two integers. When only one is given, the initial value is 0. len(text) is an integer, the length of the string, text. So given a string of length 20,

for i in range(20):

much like a JavaScript for statement,

for (var i = 0; i < text.length; i++) {}

+= is concatenation in place. We are growing a string one character at a time.

 a += b   =>  same as  a = a + b

The assignment is a single index position in the string, text[_]. The expression contained in the subscript works to evaluate to index from the right end of the string.

This line should be removed. Instead, outside of the function, print the call expression:

print reverse("reverse this text")

If it looks complex then there is a possibility of improvement. But if it works, that's a good start. Recommend scour the forums for other examples so you get a fuller understanding as a whole.


I'm new at this stuff, sorry to take your time haha

But really thanks man! You helped me a lot.


No problem, Joao! Glad to help!


can anyone please explain to me how this line of code works

 for i in range(len(text)):
     reverse += text[len(text)-1-i]

Im a bit confused with how // text[len(text)-1-i] // works in the code. Thanks in advance


The fixed value in the subscript is len(text)-1. With i initially at 0, this will be the rightmost (last) character in the string. That character becomes the first in the new string. As i increases, the index moves to the left.


I need help! This code works correctly in IDLE, but in the exersise interpreter throws following error "Does your reverse function take exactly one argument (a string)? Your code threw a "'builtin_function_or_method' object has no attribute 'getitem'" error." I really don't know what's the problem

def reverse(text):

newString = ""
for n in range(len(text) - 1, -1, -1):
    newString += next[n]
return newString



Do you mean next or text?


Oh, really :slightly_smiling: I'm so careless, thanks


Hello, im still struggling with the reversed_text += text[len(text)-1-i] could you please explain it one more time?, here, im going to explain the code the way i understand it:
on reversed_text = "" we create that variable to store our reversed word
for i in range(len(text)): it basically loops thorugh the word's index
so far so good, but why it has to be reversed_text += text[len(text)-1-i]that way?
Please help hehe



@pedrova: That's just a way João decided to do.

len(text) -1 - i

You'll bring back the value of the length of your list, subtract by one (because in programming we go from 0 to string's lenght-1) and will again subtract with the value stored in i, which will be one of the values of the list you get by using range().
After doing this calculation, you'll get the element at a certain position and assign to your new string.
At the first time the loop executes, the value in i will be zero so you'll get: text[len(text) - 1 - 0], will ge the element at that position and assign to your new string. The value of i will be increased at each loop and so you'll iterate over your original string.
Try getting the explanation I gave you and use it on the example above!

  0   1   2   3


ok, i think i got it, i will try it with your example:
TEST has a len of 4
T has an index of 0
4 - 1 - 0 = 3
reversed_text += text[3] which is equal to T, now reversed_text has T on it
E has an index of 1
4 - 1 - 1 = 2
reversed_text += text[2] which is equal to S, now reversed_text has TS on it
S has an index of 2
4 - 1 - 2 = 1
reversed_text += text[1] which is equal to E, now reversed_text has TSE on it
T has an index of 3
4 - 1 - 3 = 0
reversed_text += text[0] which is equal to T, now reversed_text has TSET on it

i see it now, thanks for breaking it down

PS: are there another ways? is kinda hard for me to see it hahahaha


@pedrova: There are other ways such as use reverse or using negative integers as index positions.
Different from other languages, such as C++/C, Python allow us to use negative integer values as index to access positions at strings/lists.

 -4  -3  -2  -1
  0   1   2   3

Example code:

stringName = "Testing!"
newString = ""
count = -1 #Means we'll start from the last position of your string
#And go to the first one, so instead of left-to-right, 
#we'll go right-to-let 
for char in stringName:
    newString += stringName[count]
    count -= 1
print (newString)


ohhh ok
thanks for all the feedback :smiley: