[Tutor] Help Needed
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"
>>> print s
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 : mystr = "I'm not a witch!"
In : mystr
Same goes for one that's defined with raw_input:
In : mystr = raw_input("She turned you into a newt? ")
She turned you into a newt? I got better!
In : mystr + mystr[1:]
Out: 'I got better!'
Python has a slice operator the colon, so mystr 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 : mystr[2:5]
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...
More information about the Tutor