RELEASED: Python 2.3a2

John Luebs jkluebs at luebsphoto.com
Thu Feb 20 22:49:39 EST 2003


Thus spake Rene Pijlman, on Thu, 20 Feb 2003 10:58:17 +0100:

> Guido van Rossum:
>>- Extended slices, e.g. "hello"[::-1] returns "olleh".
> 
> This is a troll, right? :-)

You wish.

The third parameter is the step parameter.

So 'hello'[::1] returns 'hello'. 'hello'[::2] returns 'hlo'. A negative
step value is interpreted as you would expect (step left, instead of
right), but starting from the right of the sequence. So [::-1] will
reverse, as shown above.

This syntax has been valid for a while. In fact you can try it with an
older version of Python, with the Numeric package. Initialize an array and
then take the array and do a[::-1].

Python 2.3 simply adds support for this slice feature in the builtin sequence
types.







More information about the Python-list mailing list