473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Reverse a String?

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)
Sep 23 '06 #1
8 27622
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'
Sep 23 '06 #2
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

Sep 23 '06 #3
p.*******@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>

Sep 24 '06 #4
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?

Sep 24 '06 #5
p.*******@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
Sep 25 '06 #6
p.*******@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>

Sep 25 '06 #7
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

Sep 25 '06 #8
p.*******@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>

Sep 25 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

59
by: Raymond Hettinger | last post by:
Please comment on the new PEP for reverse iteration methods. Basically, the idea looks like this: for i in xrange(10).iter_backwards(): # 9,8,7,6,5,4,3,2,1,0 <do something with i> The...
8
by: Jim Langston | last post by:
I have a class I designed that stores text chat in a std::vector<sd::string>. This class has a few methods to retrieve these strings to be displayed on the screen. void ResetRead( bool Reverse,...
21
by: google | last post by:
I'm trying to implement something that would speed up data entry. I'd like to be able to take a string, and increment ONLY the right-most numerical characters by one. The type structure of the...
20
by: sahukar praveen | last post by:
Hello, I have a question. I try to print a ascii file in reverse order( bottom-top). Here is the logic. 1. Go to the botton of the file fseek(). move one character back to avoid the EOF. 2....
24
by: Sathyaish | last post by:
This one question is asked modally in most Microsoft interviews. I started to contemplate various implementations for it. This was what I got. #include <stdio.h> #include <stdlib.h> #include...
47
by: sudharsan | last post by:
could any one please give me a code to reverse a string of more than 1MB .??? Thanks in advance
41
by: rick | last post by:
Why can't Python have a reverse() function/method like Ruby? Python: x = 'a_string' # Reverse the string print x Ruby: x = 'a_string' # Reverse the string
15
by: rajash | last post by:
Thanks for the additional comments. Here is a solution to an exercise I had problems with. I still don't think it's really what's wanted as it uses a "state variable" n - but I can't see how to...
1
by: sunnyluthra1 | last post by:
Hi, I was creating an Application in MS Access for Geocoding a particular Address from Google to get the Lat & Long. I successfully able to did that. Here is the code:...
38
by: ssecorp | last post by:
char* reverse(char* str) { int length = strlen(str); char* acc; int i; for (i=0; i<=length-1; i++){ acc = str; } return acc; }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.