Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #11177
| References | <2e5f052a-4922-433d-bbea-ec0c13e08a42@glegroupsg2000goo.googlegroups.com> <4e4344ef$0$29982$c3e8da3$5496439d@news.astraweb.com> |
|---|---|
| Date | 2011-08-10 20:49 -0700 |
| Subject | Re: Bizarre behavior of the 'find' method of strings |
| From | Chris Rebert <clp2@rebertia.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2145.1313034593.1164.python-list@python.org> (permalink) |
On Wed, Aug 10, 2011 at 7:56 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> On Thu, 11 Aug 2011 11:24 am 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.
<snip>
> "Why does slicing not work the way I expect?"
>
> Answer: because Python uses half-open slices, where the end parameter is not
> included. The reason for that is that experience with other languages shows
> that it leads to fewer "off-by-one" errors.
>
> See also:
>
> http://mail.python.org/pipermail/tutor/2010-December/080592.html
> http://en.wikipedia.org/wiki/Off-by-one_error
And further:
http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html
Cheers,
Chris
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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