Sathyaish wrote: > 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 > solution. > > > PS: Or, if my assumption that strings are immutable and an in-place > reversal is possible, is wrong, please correct me.
If you are using strings that are long enough where you're going to run into memory issues, you can create a character array from the array module. This will basically create a mutable string. -- http://mail.python.org/mailman/listinfo/python-list