Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'parameter': 0.05; 'python': 0.08; 'also:': 0.09; 'folks,': 0.09; 'subject:method': 0.09; "subject:' ": 0.15; 'included.': 0.16; 'slicing': 0.16; 'subject:Bizarre': 0.16; 'subject:behavior': 0.16; 'subject:find': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.16; 'wed,': 0.17; 'cheers,': 0.18; 'cc:no real name:2**0': 0.20; 'this?': 0.21; 'cc:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'pm,': 0.24; 'aug': 0.24; 'url:wiki': 0.25; 'url:edu': 0.28; 'thu,': 0.28; 'message- id:@mail.gmail.com': 0.29; 'cc:addr:python.org': 0.30; '"why': 0.30; 'fewer': 0.30; 'chris': 0.32; 'shows': 0.32; 'does': 0.32; "can't": 0.33; 'uses': 0.35; 'thank': 0.35; 'received:209.85.161': 0.35; 'url:python': 0.36; 'jim': 0.37; 'url:pipermail': 0.37; 'languages': 0.37; 'using': 0.37; 'something': 0.37; 'think': 0.38; 'steven': 0.38; 'received:google.com': 0.38; 'url:org': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; 'url:en': 0.39; 'help': 0.39; 'why': 0.39; 'where': 0.40; 'here': 0.65; 'greetings,': 0.66; '11:24': 0.84; 'sender:addr:chris': 0.84; 'url:tutor': 0.84; 'to:none': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:cc:content-type; bh=tfWD2GveFIvpG7Rh9rMwb9IVgdj38GctCnCp/rw16E4=; b=J8WgpmuLLtP36kPjPAaMn3wlyNhFA6M+jyPImMm93uOq8vB3NHg01CUhxmMd29n5Uj qpqob0EurWWAEM58iUrKc/ejgBIGctZBsw2zNo++AAbhRMTJ4R0E+26UKiduMjJze0jl 7PNp0B53t+lJWJKuTWeDQRaBxZqoqli2xjQpE= MIME-Version: 1.0 Sender: chris@rebertia.com In-Reply-To: <4e4344ef$0$29982$c3e8da3$5496439d@news.astraweb.com> References: <2e5f052a-4922-433d-bbea-ec0c13e08a42@glegroupsg2000goo.googlegroups.com> <4e4344ef$0$29982$c3e8da3$5496439d@news.astraweb.com> Date: Wed, 10 Aug 2011 20:49:49 -0700 X-Google-Sender-Auth: 1RW7kyS6rDKUIcAPx37r2gEy_xw Subject: Re: Bizarre behavior of the 'find' method of strings From: Chris Rebert Cc: python-list@python.org Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1313034593 news.xs4all.nl 23908 [2001:888:2000:d::a6]:46062 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:11177 On Wed, Aug 10, 2011 at 7:56 PM, Steven D'Aprano 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. > "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