One bit of Python I’ll never get…

>>> “hello”[2:4]
>>> “hello”[2:]
>>> “hello”[:4]

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).


>>> “hello”[2:4]
>>> “hello”[0]
>>> “hello”[2]
>>> “hello”[4]
>>> “hello”[-1]

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…

About jay

I'm trying to build something interactive where I can learn from others and hopefully share useful knowledge too.
This entry was posted in code, frustration, python. Bookmark the permalink.

2 Responses to One bit of Python I’ll never get…

  1. jay says:

    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!

  2. Frank Jania says:

    I dealt with this for the first time the other day writing a parser. What a weird convention.

Comments are closed.