Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.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.060 X-Spam-Evidence: '*H*': 0.88; '*S*': 0.00; 'advance': 0.07; 'string': 0.09; 'useless': 0.09; 'elements,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'simpler,': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'aug': 0.22; '31,': 0.24; 'header:In-Reply- To:1': 0.27; 'testing': 0.29; 'instruction': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'cases': 0.33; 'maybe': 0.34; 'problem': 0.35; 'but': 0.35; 'received:google.com': 0.35; '(we': 0.36; 'similar': 0.36; 'skip:- 20': 0.37; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'solve': 0.60; 'more': 0.64; 'occur': 0.65; 'between': 0.67; 'clearer': 0.84; 'subject:space': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=+5US3nt2O1gST4ZH1rbN6/mzEv8ObtRTDXxV4luK3vU=; b=MIqKJ0uG00QL3YjQu+/lCpQR/3chd+xf71oNI/GE3UoMEHKJSGgLUaKCN9XJRSVyk0 VGFCQ8YLQ8eN2VwPEcr7HF4D93J907jACp1XmVLHGn2QcXZmncXcYDJeWUkQoTv1wRf7 uGJuS5aWGMS3C6rYsj8C2U7U+DfZixPMnH5psuAm6DwCzvBZ7u2e2yNhIPh++h7rlq9K 9hdyq6oqP6M/0ZqOGErwmhLelCYAn98GQO760TvTHYweq7LgDuunSuzmGi6eIxcEMLsP rJ+QQ1X7WF2BXVvbFkZ98l5AnEFO8y+A8HzjRniWJLsclZ7hFjcHOlKB7ysgmpAHvimv dE2g== MIME-Version: 1.0 X-Received: by 10.220.74.69 with SMTP id t5mr13878821vcj.18.1377963048900; Sat, 31 Aug 2013 08:30:48 -0700 (PDT) In-Reply-To: <5221f0af$0$2412$426a34cc@news.free.fr> References: <5221a693$0$2059$426a74cc@news.free.fr> <5221b68b$0$2280$426a74cc@news.free.fr> <5221f0af$0$2412$426a34cc@news.free.fr> Date: Sun, 1 Sep 2013 01:30:48 +1000 Subject: Re: print function and unwanted trailing space From: Chris Angelico To: python-list@python.org 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: 26 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1377963057 news.xs4all.nl 15875 [2001:888:2000:d::a6]:35747 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:53365 On Sat, Aug 31, 2013 at 11:33 PM, candide wrote: > The if instruction imposes useless testing (we know in advance the problem > to occur at the very end of the loop) and useless writing (writing ''). > > The following is clearer > > # ------------------------- > n=5 > for i in range(n-1): > print(i, end=' ') > print(n-1) > # ------------------------- > > > but doesn't solve all the cases (imagine a string or an iterator). Similar but maybe simpler, and copes with more arbitrary iterables: it=iter(range(5)) print(next(it), end='') for i in it: print('',i, end='') Also guarantees to use 'sep' between the elements, fwiw. ChrisA