in-place string reversal

Sathyaish sathyaish at
Tue Mar 28 16:08:51 CEST 2006

How would you reverse a string "in place" in python? I am seeing that
there are a lot of operations around higher level data structures and
less emphasis on primitive data. I am a little lost and can't find my
way through seeing a rev() or a reverse() or a strRev() function around
a string object.

I could traverse from end-to-beginning by using extra memory:

strText = "foo"
strTemp = ""
for chr in strText:
   strTemp = chr + strTemp

but how would I do it in place?

Forget it! I got the answer to my own question. Strings are immutable,
*even* in python. Why not! The python compiler is written in C, right?
It is amazing how just writing down your problem can give you a

PS: Or, if my assumption that strings are immutable and an in-place
reversal is possible, is wrong, please correct me.

More information about the Python-list mailing list