Index errror trying to reverse string


#1

I wrote

def reverse(text):
  rev_text = ""
  for i in range(len(text)-1, 0, -1):
    rev_text += text[len(text)-1]
  return rev_text

It gave me index out of range.

I also tried

for i in range(len(text)-1, -1, -1):

for i in range(len(text)-1, 1, -1):

for i in range(len(text), 0, -1):

for i in range(len(text), -1, -1):

for i in range(len(text), 1, -1):

None of those work. I still get the ‘out of range’ error.

Any advice?


How can I loop through text starting from the last character?
#2

its not just about trying different solution, but also understanding them.

you wrote a loop, but then here:

rev_text += text[len(text)-1]

you just use len(text)-1, so you will always get the last letter of the string. Maybe add a function call so you can “see” what your function is doing?


#3

Thank you. I got it fix, but not sure I understand the concept. Here is what I understand.

What I wrote means I just keep adding the last letter of the text for n times when n is equal to the length of the text instead of make it write the last letter only once and follow by the letter of the index before the last letter.

What I should do instead of the above is

rev_text += text[i]

when i is counting down by the for loop.


Another question though, I don’t understand very clear when I tried to write for-range loop counting down from the last letter to the first.

I know that the word “Hello” 's length is 5. When I attempt to write for-range loop of

range(start, stop, step)

The ‘start’ should be the last letter of “Hello” which is “len(‘Hello’) - 1”, right? Because the first index is counting from zero.

The ‘step’ is -1 because I want to count down from the last letter which is the highest index number down to zero by one.

The ‘stop’ is the tricky one. I don’t know which one I should put there, 0 or 1 or -1?
Should I put 1 because so 1 minus by 1 is equal to 0?
Or should I put 0 there because I want it to end at 0?
Or should I put -1 there because I know that each index will be minus by 1 in which index 0 minus by 1 is equal to -1?

Which strategies should I use when dealing with this type of problem?


#4

good, you seem to grasp this concept :slight_smile:

you could look at the documentation at range? You could print i to see the values.

or just print range() with some values to see what it outputs. Range just produces a lit of numbers.


#5

I know which letter is in which index, but after counting down with the for loop, I don’t know which one I should put in the ‘stop’. Am I supposed to put the index that I want to subtract it (the index 0) by 1 in the there or should I put the result of the index 0 that is already subtracted by 1 which is -1 there?

I’ll try to look at another range document if what I don’t understand require more reading. I don’t mind reading. I just want to get to the point. Something I can easily miss when I read through a document without having a clear focus on what I am looking for.


#6

range does not include the stop value, so you need to be one lower then the lowest index


#7

That one is correct. Just use text[i]

>>> list(range(10, -1, -1))
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> 

#8

Can you post the latest version of your code? I lost track of what your code currently looks like


#9

Sure.

Here it is.

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

print reverse('hello')

#10

that looks to be working fine:

Screenshot_2018-09-06_20-09-51

are you still experiencing problems?


#11

I finally got it two days ago. Thanks anyway. For the help and quick reply.


#13