Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'one?': 0.05; 'ignored': 0.07; 'indexing': 0.07; 'string': 0.09; 'function:': 0.09; 'literal': 0.09; 'prefix': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'jan': 0.12; "'b'": 0.16; 'considers': 0.16; 'ordinal': 0.16; 'rationale': 0.16; 'surprising': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'version?': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'header:User- Agent:1': 0.23; 'byte': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'especially': 0.30; '120': 0.31; "d'aprano": 0.31; 'extract': 0.31; 'steven': 0.31; 'anyone': 0.31; 'url:python': 0.33; 'sequence': 0.36; 'hi,': 0.36; 'url:org': 0.36; 'behind': 0.37; 'url:library': 0.38; 'rather': 0.38; 'does': 0.39; 'easy': 0.60; 'length': 0.61; 'from:charset:utf-8': 0.61; 'content-disposition:inline': 0.62; 'making': 0.63; 'url:reference': 0.84; 'received:86': 0.91; 'received:hu': 0.93 X-Spam-Flag: NO X-Spam-Score: -1.912 X-Spam-Level: X-Spam-Status: No, score=-1.912 tagged_above=-20 required=4 tests=[ALL_TRUSTED=-1, AWL=0.086, BAYES_00=-1.9, DKIM_ADSP_CUSTOM_MED=0.001, FREEMAIL_FROM=0.001, NML_ADSP_CUSTOM_MED=0.9] autolearn=no Date: Tue, 7 Jan 2014 12:53:04 +0100 From: Ervin =?utf-8?Q?Heged=C3=BCs?= To: Steven D'Aprano Subject: Re: Bytes indexing returns an int References: <52cbe15a$0$29993$c3e8da3$5496439d@news.astraweb.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <52cbe15a$0$29993$c3e8da3$5496439d@news.astraweb.com> User-Agent: Mutt/1.5.20 (2009-06-14) Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1389095781 news.xs4all.nl 2962 [2001:888:2000:d::a6]:42870 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:63423 hi, On Tue, Jan 07, 2014 at 10:13:29PM +1100, Steven D'Aprano wrote: > Does anyone know what the rationale behind making byte-string indexing > return an int rather than a byte-string of length one? > > That is, given b = b'xyz', b[1] returns 121 rather than b'y'. > > This is especially surprising when one considers that it's easy to extract > the ordinal value of a byte: > > ord(b'y') => 121 Which Python version? http://docs.python.org/2/reference/lexical_analysis.html#strings "A prefix of 'b' or 'B' is ignored in Python 2;" if you want to store the string literal as byte array, you have to use "bytearray()" function: >>> a = bytearray('xyz') >>> a bytearray(b'xyz') >>> a[0] 120 >>> a[1] 121 http://docs.python.org/2/library/stdtypes.html 5.6. Sequence Types hth, a.