7/15 I'm trying to figure out a way to remove the last char in my string


Hello all,

My code is below and my issue is this: I’ve been able to successfully return the last char in a given string, and for the life of me I can’t think of a way to remove the last char so that it can successfully print all of the characters in reverse order. Can someone point me in the right direction?

def reverse (text):
reversed_text = ““
for char in text:
reversed_text += str(text [len(text)-1])
””"This is where I want to add code that will remove the last char in text
return reversed_text

*Sorry, I don’t know how to indent the text to show proper format, but please assume that:
line 2,3 indent
line 4,5 double indent
line 6 indent


You can’t. Strings are not mutable (changeable)
Instead, use slicing, or put the characters in a list, remove the last element, and then create a string from the characters you’ve still got

If you’re going to print something, then there’s no reason to remove anything, it won’t stop you from printing the next one.

Additionally, the purpose of that function isn’t to print anything, so printing isn’t sufficient, imagine, for example, that the + operator printed something every time you used it. That would be a little annoying, its purpose isn’t to display anything, it should just add. Your function should just reverse a string.


Thank you for your reply! You’re absolutely right and I mistyped when I said “print”, what I should have said and meant was “return”.

I thought I understood what you were saying about strings being immutable, except when I run the code that I have currently, the error codecademy gives me says that calling reverse (Python!) produces the result “!!!”, which makes me believe that the variable reversed_text is being mutated through my function, no?

Appreciate your thoughts on this.


Can’t change strings ever.
You can create new ones.


There’s nothing about that suggesting mutation, so you probably mean something else.

One value goes in, another goes out. They’re different, so you know that they’re not the same value because strings are immutable.


oOo, I think I understand now. While I can reverse_text += str(text [len(text)-1]), because it’s creating “new” variables each time, what you are pointing out for me is that I cannot delete/remove a piece from a string. Is that it?

When I say mutation, I figured mutable/immutable was the verbal form of mutation, but I may have been wrong.

Thank you for your advice and guidance, I was able to solve the problem after a little more tweaking.


Variables are names that can refer to values. You’re creating values, not variables.

A list is mutable, you can add and remove elements from it. A string will never change.


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