[Tutor] Counting a string backwards
Alan Gauld
alan.gauld at yahoo.co.uk
Sun May 28 19:19:40 EDT 2017
On 28/05/17 18:58, C W wrote:
> Now if I do case 2,
>> print(great[-3:-1])
>> me
>
> Where did the exclamation mark go in case 2?
>
> I was told the count begins at zero, that's true going forward, but not
> backwards.
Its not about where the count starts its about where it finishes.
It finishes 1 item before the second index. so in this case
you get the items at -3 and -2.
If you specify a third parameter you can change the
direction of the count thus:
>>> great[-1:-3:-1]
'!e'
So now you get the characters at -1 and -2 (ie in reverse order)
If you want them in original order starting at -3 just omit
the -1:
>>> great[-3:]
'me!'
HTH,
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list