I just stumbled on this Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on win32 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 <[email protected]> wrote: From: Wayne <[email protected]> Subject: Re: [Tutor] Help Needed To: "Raj Medhekar" <[email protected]> Cc: "Python Tutor" <[email protected]> Date: Monday, June 15, 2009, 5:28 PM On Mon, Jun 15, 2009 at 3:00 PM, Raj Medhekar <[email protected]> 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 -----Inline Attachment Follows----- _______________________________________________ Tutor maillist - [email protected] http://mail.python.org/mailman/listinfo/tutor
_______________________________________________ Tutor maillist - [email protected] http://mail.python.org/mailman/listinfo/tutor
