>>> “hello”[2:4]
‘ll’
>>> “hello”[2:]
‘llo’
>>> “hello”[:4]
‘hell’
Do any of those not make sense to you? Here’s how I see it, and my confusion;
“hello” represents a 5 character string… and indexing starts at 0, something most programmers are all familiar with. The “[" "]” characters are used to subscript (or index) the string (which is actually a list of characters).
So;
>>> “hello”[2:4]
>>> “hello”[0]
‘h’
>>> “hello”[2]
‘l’
>>> “hello”[4]
‘o’
>>> “hello”[-1]
‘o’
Get it? The 0th character is ‘h’, 2nd is ‘l’ (the 1st one) and the 4th (or -1 i.e. 1 from the end) character is ‘o’. The ‘:’ in between the “[]” is used to separate out the various indexing parameters.
Holistically, it’s actually referred to as “slicing“. So [1:2] says slice using indexes 1 and 2 (sorta… wait for the punchline below), and you can also say [0:20:2] which will slice in ’steps’ of 2!!
It’s a handy technique, but let’s return to our example for the rub… Notice how “hello”[2:4] gave us ‘ll’ and not characters 2,3 & 4!!! That’s because the slice includes the 1st index but excludes the second!
Man I hate that…

{ 2 } Comments
I dealt with this for the first time the other day writing a parser. What a weird convention.
Hey Frank,
Yea it’s the strangest thing and really hurts my brain sometimes. I also really dislike being forced to add a ‘:’ at the end of conditionals (and function statements) since python’s mantra was “whitespace matters” … oh well good luck w/ your parser, I find regexps fun!
Post a Comment