Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #53346
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.003 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'interpreter': 0.05; '"""': 0.07; 'interpreter.': 0.07; 'exec': 0.09; 'item.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'trailing': 0.09; 'python': 0.11; '"w")': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'statement.': 0.16; 'true:': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'finished': 0.19; 'mechanism': 0.19; '>>>': 0.22; 'aug': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'stick': 0.24; 'equivalent': 0.26; 'code:': 0.26; 'header:X-Complaints-To:1': 0.27; 'tried': 0.27; "doesn't": 0.30; 'gives': 0.31; 'code': 0.31; 'block,': 0.31; "d'aprano": 0.31; 'file:': 0.31; 'indentation': 0.31; 'steven': 0.31; 'another': 0.32; 'limitation': 0.33; 'not.': 0.33; 'could': 0.34; 'test': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; '+0200,': 0.36; 'error.': 0.37; 'skip:- 20': 0.37; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'space': 0.40; 'course': 0.61; 'back': 0.62; 'occur': 0.65; 'here': 0.66; 'results': 0.69; 'compare:': 0.84; 'subject:space': 0.84; 'try,': 0.84; 'careful': 0.91; 'items,': 0.91; '2013': 0.98 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: print function and unwanted trailing space |
| Date | Sat, 31 Aug 2013 14:27:39 +0200 |
| Organization | None |
| References | <5221a693$0$2059$426a74cc@news.free.fr> <5221d090$0$6599$c3e8da3$5496439d@news.astraweb.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p508488bf.dip0.t-ipconnect.de |
| User-Agent | KNode/4.7.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.408.1377952066.19984.python-list@python.org> (permalink) |
| Lines | 72 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1377952066 news.xs4all.nl 15960 [2001:888:2000:d::a6]:48669 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:53346 |
Show key headers only | View raw
Steven D'Aprano wrote:
> On Sat, 31 Aug 2013 10:17:23 +0200, candide wrote:
>
>> What is the equivalent in Python 3 to the following Python 2 code:
>>
>> # -----------------------------
>> for i in range(5):
>> print i,
>> # -----------------------------
>>
>> ?
>>
>> Be careful that the above code doesn't add a trailing space after the
>> last number in the list,
>
> Of course it does. Have you actually tried it? The interactive
> interpreter is tricky, because you cannot directly follow a for-loop with
> another statement. If you try, the interactive interpreter gives you an
> indentation error. But we can work around it by sticking everything
> inside an if block, like so:
>
> py> if True:
> ... for i in range(5):
> ... print i,
> ... # could be pages of code here
> ... print "FINISHED"
> ...
> 0 1 2 3 4 FINISHED
>
>
> Or you could stick the code inside an exec, which doesn't have the same
> limitation as the interactive interpreter. This mimics the behaviour of
> code in a file:
>
> py> exec """for i in range(5):
> ... print i,
> ... print "FINISHED"
> ... """
> 0 1 2 3 4 FINISHED
>
>
> The same results occur with any other Python 2.x, and indeed all the way
> back to Python 1.5 and older.
Your test is flawed. The softspace mechanism ensures that there is a space
*between* all printed items, but not *after* the last printed item.
print "FINISHED"
will add a space while
print
will not. Compare:
>>> with open("tmp.txt", "w") as f:
... for i in range(3): print >> f, i,
... print >> f
...
>>> open("tmp.txt").read()
'0 1 2\n'
>>> with open("tmp.txt", "w") as f:
... for i in range(3): print >> f, i,
... print >> f, "FINISHED"
...
>>> open("tmp.txt").read()
'0 1 2 FINISHED\n'
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
print function and unwanted trailing space candide <candide@free.invalid> - 2013-08-31 10:17 +0200
Re: print function and unwanted trailing space Andreas Perstinger <andipersti@gmail.com> - 2013-08-31 10:43 +0200
Re: print function and unwanted trailing space candide <candide@free.invalid> - 2013-08-31 11:25 +0200
Re: print function and unwanted trailing space Peter Otten <__peter__@web.de> - 2013-08-31 12:31 +0200
Re: print function and unwanted trailing space candide <candide@free.invalid> - 2013-08-31 15:33 +0200
Re: print function and unwanted trailing space Peter Otten <__peter__@web.de> - 2013-08-31 15:59 +0200
Re: print function and unwanted trailing space candide <candide@free.invalid> - 2013-08-31 17:58 +0200
Re: print function and unwanted trailing space Chris Angelico <rosuav@gmail.com> - 2013-09-01 01:30 +1000
Re: print function and unwanted trailing space Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-08-31 16:43 +0100
Re: print function and unwanted trailing space Chris Angelico <rosuav@gmail.com> - 2013-09-01 08:08 +1000
Re: print function and unwanted trailing space Joshua Landau <joshua@landau.ws> - 2013-09-01 00:15 +0100
Re: print function and unwanted trailing space Terry Reedy <tjreedy@udel.edu> - 2013-08-31 19:57 -0400
Re: print function and unwanted trailing space candide <candide@free.invalid> - 2013-08-31 17:51 +0200
Re: print function and unwanted trailing space Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-31 11:16 +0000
Re: print function and unwanted trailing space Peter Otten <__peter__@web.de> - 2013-08-31 14:27 +0200
Re: print function and unwanted trailing space Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-08-31 13:37 +0100
Re: print function and unwanted trailing space candide <candide@free.invalid> - 2013-08-31 14:59 +0200
Re: print function and unwanted trailing space Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-31 15:18 +0000
Re: print function and unwanted trailing space Ned Batchelder <ned@nedbatchelder.com> - 2013-08-31 07:24 -0400
Re: print function and unwanted trailing space candide <candide@free.invalid> - 2013-08-31 15:51 +0200
Re: print function and unwanted trailing space Wayne Werner <wayne@waynewerner.com> - 2013-09-11 06:36 -0500
Re: print function and unwanted trailing space Albert Hopkins <marduk@letterboxes.org> - 2013-09-12 15:25 -0400
csiph-web