Reverse a String?

p.lavarre at ieee.org p.lavarre at ieee.org
Sat Sep 23 17:34:59 EDT 2006


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/

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()

3) I went looking when first I found time to rethink what I had been
running this past year:

	def reverse(chars):
		ochars = ''
		beyond = len(chars)
		for ix in range(beyond):
			ochars += chars[beyond - 1 - ix]
		return ochars




More information about the Python-list mailing list