in-place string reversal
Jean-Paul Calderone
exarkun at divmod.com
Tue Mar 28 09:25:43 EST 2006
On 28 Mar 2006 06:08:51 -0800, Sathyaish <sathyaish at gmail.com> 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
>
This is much slower than strText[::-1] (or any other squence), but which also isn't in-place.
In-place operations on strings aren't support. You may want to consider a different datatype if this is important. For example, an array of characters.
Jean-Paul
More information about the Python-list
mailing list