[Tutor] Counting a string backwards

C W tmrsg11 at gmail.com
Sun May 28 20:11:35 EDT 2017


Hi Alan

Thank you very much, I got it. So in this case, there is no need to specify
where it ends.

In fact, even if I wanted to specify the ending, I can't!

Thank you!

On Sun, May 28, 2017 at 7:19 PM, Alan Gauld via Tutor <tutor at python.org>
wrote:

> 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
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list