[Tutor] Newb ?

Christian Wyglendowski Christian.Wyglendowski at greenville.edu
Thu Nov 17 22:08:15 CET 2005

Orri said:
> Or you could just do the following:
> >>> print "\n\nWelcome to the Backwards Message Display."
> >>> print
> >>> message = raw_input("\nPlease Enter a Message.")
> >>> print message[::-1]

Interesting.  I forgot about the 'step' option when slicing.
> This is the equivalent of print ''.join(reversed(message)), since 
> reversed works on any iterable sequence, including strings.  

Ha!  Good call.  Makes it even clearer.

> In any 
> case, the syntax for this sort of thing in general is: 
> sequence[start:stop:step], with start defaulting to 0, step 
> defaulting 
> to sys.maxint (which, for all intents and purposes, means the 
> end of the 
> string), and step defaulting to 1.  However, when step is negative, 
> start and end switch defaults.  So by doing [::-1], you're telling 
> Python to return the values of the sequence that can be found 
> from the 
> end to the start

Cool.  Thanks for this explanation.


More information about the Tutor mailing list