Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reverse a String?

5 views
Skip to first unread message

Gregory Piñero

unread,
Sep 23, 2006, 5:02:16 PM9/23/06
to pytho...@python.org
Is my mind playing tricks on me? I really remember being able to
reverse a string as in:

text='greg'
print text.reverse()
>> 'gerg'

Is it possible thats in some Python install and not in others? I just
switched to linux.

In any case, can we get that added?

Here's my workaround for now:
def reverse(text):
return ''.join([text[i] for i in range(len(text)-1,-1,-1)])

--
Gregory Piñero
Chief Innovation Officer
Blended Technologies
(www.blendedtechnologies.com)

Robert Kern

unread,
Sep 23, 2006, 5:04:18 PM9/23/06
to pytho...@python.org
Gregory Piñero wrote:
> Is my mind playing tricks on me? I really remember being able to
> reverse a string as in:
>
> text='greg'
> print text.reverse()
>>> 'gerg'
>
> Is it possible thats in some Python install and not in others? I just
> switched to linux.
>
> In any case, can we get that added?

Not in that form, no, since this already exists:

text[::-1]

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Leif K-Brooks

unread,
Sep 23, 2006, 5:04:57 PM9/23/06
to
Gregory Pińero wrote:
> Is my mind playing tricks on me? I really remember being able to
> reverse a string as in:
>
> text='greg'
> print text.reverse()
>>> 'gerg'

That method has never existed AFAIK. Maybe you're thinking of the
reverse() method on lists?

In any case, the you can reverse strings in a couple of ways:

>>> ''.join(reversed('foo'))
'oof'
>>> 'foo'[::-1]
'oof'

p.la...@ieee.org

unread,
Sep 23, 2006, 5:34:59 PM9/23/06
to
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

Gregory Piñero

unread,
Sep 23, 2006, 5:54:32 PM9/23/06
to Robert Kern, pytho...@python.org
On 9/23/06, Robert Kern <rober...@gmail.com> wrote:
> Not in that form, no, since this already exists:
>
> text[::-1]

Wow, that's really cool! Here are my questions:

1. How long has this thing been going on? I didn't know slice even
took an extra argument like that.

2. Where can I get the lowdown on everything there is to know about
slice? Since I've obviously been living in ignorance all these years!

Thanks again,

Greg

Robert Kern

unread,
Sep 23, 2006, 6:06:26 PM9/23/06
to pytho...@python.org
Gregory Piñero wrote:
> On 9/23/06, Robert Kern <rober...@gmail.com> wrote:
>> Not in that form, no, since this already exists:
>>
>> text[::-1]
>
> Wow, that's really cool! Here are my questions:
>
> 1. How long has this thing been going on? I didn't know slice even
> took an extra argument like that.

Slicing took that "step" argument since 1.4. However, only Numeric arrays used
it up until 2.3 where it was added to the builtin types.

> 2. Where can I get the lowdown on everything there is to know about
> slice? Since I've obviously been living in ignorance all these years!

http://www.python.org/doc/2.3.5/whatsnew/section-slices.html

Generally the "What's New" documents are very good for catching up. I've found
that they're also a very good, high-density method of refreshing one's memory
about useful little tidbits. Sadly, they're not all collected in one place for
that purpose. But you can fake it a bit by replacing <version> in the following
URL with each of 2.2.3, 2.3.5, 2.4.3, 2.5:

http://www.python.org/doc/<version>/whatsnew/

Gregory Piñero

unread,
Sep 23, 2006, 6:51:28 PM9/23/06
to Robert Kern, pytho...@python.org
Thanks guys, and now the world knows:
http://www.answermysearches.com/index.php/super-easy-way-to-reverse-a-string-in-python/188/

Well my 3 blog readers or the world ... not sure.

-Greg

Fredrik Lundh

unread,
Sep 24, 2006, 4:52:07 AM9/24/06
to pytho...@python.org
p.la...@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>

p.la...@ieee.org

unread,
Sep 24, 2006, 6:13:12 PM9/24/06
to
> http://pyfaq.infogami.com/

Tell me more?

Clueless newbie me, thru this door I'm at three deaths and counting.
Does that Py Faq Wiki have a sandbox a la alt.test, and/or a tutorial?

// Death One:

http://pyfaq.infogami.com/_account/in?path=/
requires me to create a persisting ID "between 3 and 20 letters and
numbers", i.e., not an e-mail address. Grumble, ok.

// Death Two:

http://pyfaq.infogami.com/programming-index
kicked back comment "How can I reverse a string, an array, a list,
etc.?" with "Sorry, that looks like spam. I'm not going to post it."

// Death Three:

Search infogami in clp gives me:
http://howto.infogami.com/createnewpages
which suggests:
http://pyfaq.infogami.com/how-do-i-reverse
which dies at "Sorry, that page could not be found."

//

Next clue?

Georg Brandl

unread,
Sep 25, 2006, 3:16:47 AM9/25/06
to
p.la...@ieee.org wrote:
>> http://pyfaq.infogami.com/
>
> Tell me more?
>
> Clueless newbie me, thru this door I'm at three deaths and counting.
> Does that Py Faq Wiki have a sandbox a la alt.test, and/or a tutorial?
>
> // Death One:
>
> http://pyfaq.infogami.com/_account/in?path=/
> requires me to create a persisting ID "between 3 and 20 letters and
> numbers", i.e., not an e-mail address. Grumble, ok.
>
> // Death Two:
>
> http://pyfaq.infogami.com/programming-index
> kicked back comment "How can I reverse a string, an array, a list,
> etc.?" with "Sorry, that looks like spam. I'm not going to post it."

I must say that this has also happened to me before. Fredrik, is there
some description of what infogami regards as spam? Do comments have to
have a minimum length?

That said, hasn't it worked well in the past to use no spam filter, if
you have to be registered to post comments?

Georg

Fredrik Lundh

unread,
Sep 25, 2006, 8:30:13 AM9/25/06
to pytho...@python.org
p.la...@ieee.org wrote:

> Next clue?

I would have hoped that the suggestion links on the first page would have sent
you to

http://pyfaq.infogami.com/suggest

which is the "right" place to submit new entries. that spam thing looks weird,
though; guess I'll have to check with Aaron.

</F>

p.la...@ieee.org

unread,
Sep 25, 2006, 1:44:32 PM9/25/06
to
> http://pyfaq.infogami.com/suggest

> is the "right" place to submit new entries.

Thanks for sharing the secret. Now there I have added: "Q: How can I


reverse a string, an array, a list, etc."

And to share our secret with future newbies, to
http://pyfaq.infogami.com/ I've added the suggestion:

///

How about here we add a bold:

If you have a suggested FAQ, click thru the Suggest New Articles link
and add a comment there. Don't choose the relevant category and then
discover you lack the privilege to comment there. Don't give up because
you have no ID: just invent one and register your e-mail and proceed.

///

> I would have hoped that the suggestion links on the first page would have sent you

I actually did visit the right place - it appears listed as only the
fourth "resource" behind the label "Suggest New Articles". But the
only suggestions that were FAQs were from the effbot and submitted with
a font I didn't know how to create. Also I had no sure idea of what
"resource", "category", and "article" meant in this context, so I could
barely decipher that only-apparently-English page.

Instead, because the site looks like a Wiki, I was trying to add my
suggested FAQ in place in the relevant category. The place I picked
that choked irrecoverably was the "Programming FAQ" category, just
ahead of the "How do I modify a string in place?" question, in between
the "How do I convert a string to a number?" and "How do I convert a
number to a string?" questions.

Pat LaVarre

Fredrik Lundh

unread,
Sep 25, 2006, 2:03:55 PM9/25/06
to pytho...@python.org
p.la...@ieee.org wrote:

> Thanks for sharing the secret. Now there I have added: "Q: How can I
> reverse a string, an array, a list, etc."

thanks! I'll take another look at the texts and the site settings when
I find the time (still waiting for the infogami folks to get back to me
about the spam filter...)

</F>

0 new messages