Reverse a String?
Fredrik Lundh
fredrik at pythonware.com
Sun Sep 24 04:52:07 EDT 2006
p.lavarre at ieee.org wrote:
> That 'foo'[::-1] is the Python reverse string idiom I'll try here
> forward, thanks.
>
> Also '.h.e.l.l.o'[1::2] to pick out every second char, etc., thanks.
>
> Three footnotes:
>
> 1) Reverse string isn't yet in http://www.python.org/doc/faq/
so what are you waiting for?
http://pyfaq.infogami.com/
> 2) Google Groups searches here yesterday instead pushed me towards the
> less concise:
>
> def reverse(chars):
> aa = array.array('c', chars)
> aa.reverse()
> return aa.tostring()
that's a reasonably efficient way to do this in older versions.
</F>
More information about the Python-list
mailing list