Reverse Iteration Through Integers

Chris Rebert clp2 at rebertia.com
Mon Oct 19 02:00:24 EDT 2009


On Sun, Oct 18, 2009 at 10:53 PM, Jabba Laci <jabba.laci at gmail.com> wrote:
> Hi,
>
> Would someone explain how str[::-1] work? I'm new to Python and I only
> saw so far the str[begin:end] notation. What is the second colon?

Specifies the step value, as in:   foo[start:stop:step]

When not specified it, defaults to 1.
So foo[::-1] gives the entire sequence backwards.
Analogously, foo[::2] gives every other item in the sequence, forwards.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list