Slicing and offsets

Steven D. Majewski sdm7g at virginia.edu
Thu Jan 18 15:31:20 EST 2001


On Thu, 18 Jan 2001 gustabares at my-deja.com wrote:

>  I got a newbie question here...
> 
> >>> S = "spam"
> >>> S[0]
> 's'
> >>> S[1:]
> 'pam'
> >>>
> 
> Why is it that when I slice S it's no longer the 0 offset anymore, but
> now it is 1?
> 
> >>> S[0:]
> 'spam'
> 
> Thanks for any help and sorry for the simple question:)
> 

Not sure that I understand the question, but one of the docs 
suggests that you think of the indexes as separating the elements


	0 1 2 3 4
	|s|p|a|m|
       -4-3-2-1

( Thus there's another opportunity for Tim to complain about no -0 ! )

Indexing gives you the element immediately after the index:

"spam"[1] == "spam"[-3] == "p" 
"spam"[4] --> Index Error 

Slicing gives you the elements between the indexes:

"spam"[1:3] == "spam"[-3:-1] == "pa" 


-- Steve Majewski







More information about the Python-list mailing list