[Tutor] Help Needed

Gil Cosson gilcosson_2000 at yahoo.com
Tue Jun 16 08:53:03 CEST 2009

I just stumbled on this

Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on
Type "help", "copyright", "credits" or "license" for more information.
>>> s="hello world"
>>> s=s[::-1]
>>> print s
dlrow olleh


Gil Cosson

Bremerton, Washington

360 620 0431

--- On Mon, 6/15/09, Wayne <srilyk at gmail.com> wrote:

From: Wayne <srilyk at gmail.com>
Subject: Re: [Tutor] Help Needed
To: "Raj Medhekar" <cosmicsand27 at yahoo.com>
Cc: "Python Tutor" <tutor at python.org>
Date: Monday, June 15, 2009, 5:28 PM

On Mon, Jun 15, 2009 at 3:00 PM, Raj Medhekar <cosmicsand27 at yahoo.com> wrote:

I am looking to build a program that gets a message from the user and then prints it backward. I 've been at it for hours now but I can't seem to figure it out. I've been having trouble trying to index the message so I can print it out backwards. I would've posted my code but I really haven't gotten past the  'raw_input("Enter a message: ")' command line.  Any help is gladly appreciated. Thanks!

Python treats a string like a list/array.
In [1]: mystr = "I'm not a witch!"

In [2]: mystr[0]
Out[2]: 'I'

Same goes for one that's defined with raw_input:

In [4]: mystr = raw_input("She turned you into a newt? ")
She turned you into a newt? I got better!

In [5]: mystr[0] + mystr[1:]
Out[5]: 'I got better!'

Python has a slice operator the colon, so mystr[0] gives me the character at 0, and mystr[1:] gives me everything from the first character to the end (if you add something after the colon it will only return whatever is up to that point:

In [10]: mystr[2:5]
Out[10]: 'got'

If you know what a loop is, you should be able to figure out how to get the result you're looking for.


-----Inline Attachment Follows-----

Tutor maillist  -  Tutor at python.org

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090615/740e5767/attachment.htm>

More information about the Tutor mailing list