Reverse a String?

  • Thread starter =?ISO-8859-1?Q?Gregory_Pi=F1ero?=
  • Start date
?

=?ISO-8859-1?Q?Gregory_Pi=F1ero?=

Is my mind playing tricks on me? I really remember being able to
reverse a string as in:

text='greg'
print text.reverse()
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 for i in range(len(text)-1,-1,-1)])
 
P

p.lavarre

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
 
F

Fredrik Lundh

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

p.lavarre

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?
 
G

Georg Brandl

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
 
P

p.lavarre

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
 
F

Fredrik Lundh

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top