def reverse(text):
    for i in text:
        for i in range(len(text),-1,-1):
        return total

what is wrong in this? it shows error object has no attribute


append() is a method, it requires parentheses

you have two loops, why? A single loop should be enough


okay but this prints the length or the position of the digits in reverse order.
how to write the correct code using the above loop??
and also how to remove the [] brackets in output
please help


def reverse(text):
    for i in range(len(text)-1,-1,-1):
        total = total + (text[i])
    return total
  • changed total to be a string
  • removed the redundant first for loop
  • have the for loop start at len(text)-1 instead of len(text) as the last index is one lower then the length
  • removed append (which does not work with strings) and used '+' to add the characters to total

removed my own solution to not confuse too much as mentioned by stetim


please post an updated version of your code. You could use .join() to join the list into a string

@terarunner78986, why deviate so much from the code the learner has? That is confusing, and given strings are immutable in python, its not very memory friendly code


Hey stetim,

I didn't think my code was that far away from systemjumper's. But now that you mention the .join function I see how this would work fine using the list approach. As I am also learning, I just wanted to show the way I solved this and hoped it could be of any help. Regarding "memory friendly": I guess we're not supposed to know such stuff at this point, at least I have no idea what kind of code is more or less efficient.

But as you mentioned that strings are immutable, I got a question: If I understand correctly it means that you can't alter a string once created. In that case, why exactly can I do something like:

s = "Hi"
s += " Ted"

Isn't that changing the state of s? Thanks in advance for any clarification.


it does, but a new string is created with the joined result. lets say each character is one byte, then Hi is 2 bytes and " Ted" is 4 bytes (space is a byte as well)

this means, in total you have 8 bytes (2 for old string, and 6 for new string)

in a list, you can just add to list, so you would have 6 bytes total

now, for a program this small, this isn't very troubling. However, if your program becomes bigger, this becomes a problem.

And in your reverse approach, you don't create a new string once, but many times


def reverse(text):
for i range(len(text)-1,-1,-1):
return a

okay if i use join..still not working?


thanks it did work.
but i need to understand the join method as well when i use it in my code mentioned below

def reverse(text):
for i range(len(text)-1,-1,-1):
return a


As you can see here, you should try


(instead of using the + symbol)

.. or directly return "".join(total) if you want it to be a little shorter


def reverse(text):
    for i in range(len(text)-1,-1,-1):
        return "".join(total)

returns with error list index out of range
what should i put in the for loop to make this run?
please help


what are you doing here:


i would append to total instead

a function ends the moment a return keyword is reaches, so you want to place the return keyword outside the loop


def reverse(text):
for i in range(len(text)-1,-1,-1):
return "".join(total)

thanks i get it now...and worked..earlier i was trying to append i but i had to use text[i] instead
you're awesome.


I'm the begginer too,
Why is there 3 times -1?

for e in range(len(texte)-1,-1,-1):
I do understand first two (because string goes from 0 to end) but third one in defining range?


general syntax:

range(start, stop, step)

well, length start counting at one, where as list are zero indexed based. So you need to subtract one to compensate for this difference

range does not include its stop value, so you need -1, using 0 would mean the the first letter of your word doesn't get appended

by default, range has steps of +1, but given we are going from higher to lower value we need to specify that we want negative steps/strides


Oh, I foget for step.


Oops, try again. Your function fails on reverse("Python!"). It returns "!" when it should return "!nohtyP".

m getting this error with this code!!


then the indention of the code is not correct, if you need help, please make a new topic. This will give you a template which will ask for all information we need to help you


your indentation must be wrong
post your code here


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.