[Tutor] Help Needed

Wayne srilyk at gmail.com
Mon Jun 15 23:28:11 CEST 2009


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.

HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090615/fc0d364d/attachment.htm>


More information about the Tutor mailing list