Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #11166

Re: Bizarre behavior of the 'find' method of strings

Date 2011-08-11 02:48 +0100
From MRAB <python@mrabarnett.plus.com>
Subject Re: Bizarre behavior of the 'find' method of strings
References <2e5f052a-4922-433d-bbea-ec0c13e08a42@glegroupsg2000goo.googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.2137.1313027314.1164.python-list@python.org> (permalink)

Show all headers | View raw


On 11/08/2011 02:24, Jim wrote:
> Greetings, folks,
>
> I am using python 2.7.2. Here is something I got:
>>>> a = 'popular'
>>>> i = a.find('o')
>>>> j = a.find('a')
>>>> a[i:j]
> 'opul'
>
> Well, I expected a[i:j] to be 'opula', and can't think of any reason
> why this is not happening. So, can anybody help me out about this?
 > Thank you very much.
>
Python uses half-open ranges, which means that the start position is
inclusive and the end position is exclusive.

This means that a[i:j] returns the string starting at position i and
extending upto, but excluding, position j.

The reason that Python uses half-open ranges is that experience (with
the language Mesa) has shown that it results in fewer programming
errors that the alternatives.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Bizarre behavior of the 'find' method of strings Jim <jianbao.tao@gmail.com> - 2011-08-10 18:24 -0700
  Re: Bizarre behavior of the 'find' method of strings MRAB <python@mrabarnett.plus.com> - 2011-08-11 02:48 +0100
  Re: Bizarre behavior of the 'find' method of strings Chris Angelico <rosuav@gmail.com> - 2011-08-11 03:14 +0100
  Re: Bizarre behavior of the 'find' method of strings Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-11 12:56 +1000
    Re: Bizarre behavior of the 'find' method of strings Chris Rebert <clp2@rebertia.com> - 2011-08-10 20:49 -0700
      Re: Bizarre behavior of the 'find' method of strings Jim <jianbao.tao@gmail.com> - 2011-08-10 21:29 -0700
      Re: Bizarre behavior of the 'find' method of strings Jim <jianbao.tao@gmail.com> - 2011-08-10 21:29 -0700

csiph-web