Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'defaults': 0.05; "'a'": 0.07; 'operand': 0.07; 'parameter': 0.07; 'strings.': 0.07; 'python': 0.09; 'empty,': 0.09; 'restriction': 0.09; 'sep': 0.09; 'typeerror:': 0.09; "'b'": 0.16; "'b',": 0.16; "'c'": 0.16; "'c',": 0.16; "'d',": 0.16; "'e',": 0.16; "'int'": 0.16; '0).': 0.16; 'efficiency.': 0.16; 'instead:': 0.16; 'unsupported': 0.16; 'wed,': 0.16; 'string': 0.17; 'wrote:': 0.17; '>>>': 0.18; '(not': 0.20; 'equivalent': 0.20; 'doc': 0.22; 'header:In-Reply-To:1': 0.25; '(which': 0.26; 'expand': 0.26; 'am,': 0.27; 'wonder': 0.27; 'message-id:@mail.gmail.com': 0.27; '>>>>': 0.29; "skip:' 10": 0.30; 'notes': 0.30; 'error': 0.30; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'sequence': 0.35; 'received:209.85': 0.35; 'add': 0.36; 'should': 0.36; 'subject: (': 0.36; 'does': 0.37; 'why': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'hello,': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'sum': 0.66; 'start.': 0.84; 'strings)': 0.84; 'to:name:python': 0.84; 'type(s)': 0.84; 'inefficient': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=/BYPxR3nsAdmK2MsSM7W7TUCueV/WukFrHfAEp/t1sM=; b=scSot0ASiHQiQrMf17w3i9pokOX3ONPOFOiNT36R+aT/AQF6v0OoCuS4JADgPrDyMD mPUDmF5Jtu/Tlm0DTmNizfvTN6meFWRwtrKzGUVjeJ4jw4DZlpobo39so3czvvKBoLrY +51e31rHJM+q4t+8jrsG/pEk6tTQ+hmba9YxKy7ENbzxW0+Qd+rAObnOv/ixh/gYIG0m f1ZlJxkBYOa+cogllgHky7YZtgtwCFhiv9360v8N+XkaL9sa6Rsnh5K2nkvX7nh2NoK4 mBkih7WDc+/4XiB2m1J5J/Xigvgk89H8xXyIc1qZlrrZlpvJ/qfJ+tFED0xmqhOrWRsq kXCw== MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Wed, 19 Sep 2012 09:03:03 -0600 Subject: Re: sum works in sequences (Python 3) To: Python Content-Type: text/plain; charset=ISO-8859-1 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: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1348067016 news.xs4all.nl 6986 [2001:888:2000:d::a6]:43561 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:29497 On Wed, Sep 19, 2012 at 8:41 AM, Franck Ditter wrote: > Hello, > I wonder why sum does not work on the string sequence in Python 3 : > >>>> sum((8,5,9,3)) > 25 >>>> sum([5,8,3,9,2]) > 27 >>>> sum('rtarze') > TypeError: unsupported operand type(s) for +: 'int' and 'str' > > I naively thought that sum('abc') would expand to 'a'+'b'+'c' > And the error message is somewhat cryptic... It notes in the doc string that it does not work on strings: sum(...) sum(sequence[, start]) -> value Returns the sum of a sequence of numbers (NOT strings) plus the value of parameter 'start' (which defaults to 0). When the sequence is empty, returns start. I think this restriction is mainly for efficiency. sum(['a', 'b', 'c', 'd', 'e']) would be the equivalent of 'a' + 'b' + 'c' + 'd' + 'e', which is an inefficient way to add together strings. You should use ''.join instead: >>> ''.join('abc') 'abc'