[Tutor] SLICING
Alan Gauld
alan.gauld at yahoo.co.uk
Fri Jun 15 14:44:40 EDT 2018
On 15/06/18 14:57, kenneth yashim wrote:
> please im new to python or any other programming language. please i want to
> understand how slicing works
>
> [start,stop,step]
>
>>>> 'abc'[0:3]
> 'abc'
>
>>>> 'abc'[0:-1]
> 'ab'
>
> why does 'abc'[2:1] or 'abc'[2:1] print ' ' instead of 'c'???
Because stop is lower than start and the step is implicitly +1,
so the slice doesn't include anything.
'abc'[2:]
will yield 'c' because it implicitly specifies stop as the
end of the string.
'abc'[2:1:-1]
will also yield 'c' because the -1 step value reverses the
direction.
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