Newbie: Simple question about string slices

Sean Blakey sblakey at freei.net
Fri Jun 9 01:44:57 EDT 2000


On Fri, Jun 09, 2000 at 02:47:02PM +1000, Ray Smith wrote:
> Hi there,
> 
> I'm completely new to Python and was reading the standard tutorial for
> Python and came across string slices., The tutorial says:
> 
> "Strings can be subscripted (indexed); like in C, the first character of a
> string has subscript (index) 0. There is no separate character type; a
> character is simply a string of size one. Like in Icon, substrings can be
> specified with the slice notation: two indices separated by a colon."
> 
> Then later:
> 
> "Here's a useful invariant of slice operations: s[:i] + s[i:]equals s."
> 
> Now,
> if I have a string defined as
> 
> s = "abcdefg"
> 
> I assumed s[2:2] to be equal to 'c',
> but when I tried it it equals '' (empty).
> The way I read this is give me the slice from index 2 to index 2.
> 
> I also "feel like" s[:2] + s[2:] should be equal to 'abccdef'
> 
> Simple testing shows me
> s[2:3] = 'c', and
> s[:2] + s[2:] = "abcdef"
> 
> My question is:
> If given a slice s[a:b]
> can I assume index "b" means upto but not including the index "b"?
> 
> eg.  s="abcdef"
> 
> s[:3] = "abc", meaning from index 0, upto but not including index 3
> s[3:] = "def", meaning from index 3 to the end of the string
> 
> I guess another way to ask my question is:
> Why is s[a:a] always '' (empty) ?
> 
> Hope someone out there in Python land can make sense of my question!
> 
> Thanks
> 
> Ray Smith
> 
> 
> 
> 
> 
> 
> -- 
> http://www.python.org/mailman/listinfo/python-list

Probably the easiest way to make sence of slice semantics is to think of
the slice indexes as counting the borders between values.  For example,
s[0:] is the slice from before the first character on. s[2:2] is the
"space" number two, which is between the elements of index 1 and two.

You have probvbly seen magic acts where a person is locked into a
segmented box.  The magician then places blades between the various
segments.

Think of a python sequence as beinglike this box.  Slice indexes do not
count the sub-boxes, but the spaces between the boxes where blades can
be inserted.  You can take your slice from above the head to just above
the ankles (body[0:-1]), or from the neck to below the feet (body[1:]),
or from the neck to the ankles (body[1:-1]).  If you insert two blades
at the neck (body[1:1]), you end up with nothing between them.

Slice indexes and direct indexes are different.  Perhaps it would help
to visualize your string like this:

0   1   2   3   4   5   6
| a | b | c | d | e | f |
  0   1   2   3   4   5

With this in mind, let's try to explain your examples:
s[2:2] == ''
The slice from seperatror 2 to seperator two is a zero-length string.
This is the slice starting at the ceperator between 'b' and 'c', and
ending at the same place.

s[:2] + s[2:] == s	(a very nice feature of python, BTW)
Everything up to seperator 2, plus everything after seperator two.

In summary, adn to actually get around to your question, s[:b] meens up
to seperator b, which is just before index b.  Similarly, s[b:] means
everything after seperator b, which is just before index b.

Perhaps this will help, perhaps this will just confuse you more.
    -Sean
-- 
Sean Blakey, sblakey at freei.com
Software Developer, FreeInternet.com
(253)796-6500x1025
A debugged program is one for which you have not yet found the conditions
that make it fail.
		-- Jerry Ogdin




More information about the Python-list mailing list