[Tutor] Re: (no subject)

Andrei project5 at redrival.net
Thu Apr 14 09:08:52 CEST 2005


Jim and Laura Ahl <jcahl <at> psci.net> writes:

> How come when I ask it to print i[2:4] from an inputted string it gives me the
letters between two and four
> 
> But when I ask it to print i[-1:-4] it does not print anything.

Because the range is counting forward from -1 (the last element) onward, and
since there's nothing after the last element, it can't print anything. It's the
same as typing range(5,2) for example. If you want it to give you the letters
beteen -1 and -4 backwards, type i[-1:-4:-1] (I'm not sure that works in older
Pythons). If you need the last 4 letters, type i[-4:].

Yours,

Andrei



More information about the Tutor mailing list