[Tutor] Beginner Question

Josh F jj_frap at hotmail.com
Sun Jun 18 22:57:32 CEST 2006


re. Beginner Question

I'm still in the fairly early stages of learning Python, but I have an 
intermediate level of HTML, CSS, and VB6 knowledge along with the 
fundamentals of C and C++ and a very little bit of PERL..

But enough about me...Here's the solution.

message = raw_input("Enter your message:")
print message[-1::-1]

Slicing a string takes three arguments, all of which are optional: The first 
character to be sliced, the last character to be sliced, and tio slice every 
nth character.

In Python strings, -1 refers to the last character in the string and thus 
becomes the first argument.

You don't specify the second argument because you're printing the string 
from end to beginning.

Finallly, the -1 in the 3rd argument indicates that you want the string to 
be printed in reverse order (i.e. iterate backwards by one character).







>Message: 1
>Date: Sun, 18 Jun 2006 05:58:38 -0500
>From: "Bill Tatum" <btatum at cddn.com>
>Subject: [Tutor] Beginner question(s)
>To: <tutor at python.org>
>Message-ID: <20060618105838.0DB326318 at mem01.ecsis.net>
>Content-Type: text/plain; charset="us-ascii"
>
>Hi,
>
>
>
>I'm working through Python Programming for the Absolute Beginner.  One of
>the challenges at the end of chapter 4 is to get a message from the user 
>and
>then print it out backwards. I have.
>
>
>
>message = raw_input("Enter your message:")
>
>count = len(message)
>
>print count
>
>
>
>which gives me the number of characters in the message.  I tried to use a
>for loop:
>
>
>
>for i in range(len(message)-1,-1, -1):
>
>     print i,
>
>
>
>but I just get the numeric values of the string.  Can anyone help?
>
>
>
>Also, does anyone know of a PDA that would run python?
>
>
>
>TIA




More information about the Tutor mailing list