Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41747

Re: how does the % work?

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <tampucciolina@libero.it>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'none,': 0.05; 'correct.': 0.07; 'exception.': 0.07; 'raises': 0.07; 'python': 0.09; 'brackets': 0.09; 'function:': 0.09; 'none.': 0.09; 'received:192.168.1.101': 0.09; '"none': 0.16; '%s,': 0.16; '(name,': 0.16; 'carefully.': 0.16; 'quest': 0.16; 'wrote:': 0.17; 'fix': 0.17; 'string,': 0.17; 'tim': 0.18; '>>>': 0.18; 'example': 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'wrote': 0.26; 'first,': 0.27; 'lines': 0.28; '-0700,': 0.29; "d'aprano": 0.29; 'statements': 0.29; 'steven': 0.29; 'no,': 0.29; 'fri,': 0.30; 'function': 0.30; 'error': 0.30; 'code': 0.31; 'operate': 0.32; 'print': 0.32; 'to:addr:python- list': 0.33; 'clear': 0.35; 'so,': 0.35; 'subject:?': 0.35; 'add': 0.36; 'thank': 0.36; 'two': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'called': 0.39; 'where': 0.40; 'received:192.168': 0.40; 'your': 0.60; 'first': 0.61; 'between': 0.63; 'color': 0.69; '2013': 0.84; 'all!': 0.84; 'received:212.52.84': 0.84; 'received:libero.it': 0.84; 'received:212.52': 0.91
X-CTCH-Spam Unknown
X-CTCH-RefID str=0001.0A0C0205.514DE0D4.00EF,ss=1,re=0.000,fgs=0
X-libjamoibt 1823
Date Sat, 23 Mar 2013 18:05:20 +0100
From leonardo <tampucciolina@libero.it>
User-Agent Mozilla/5.0 (Windows NT 6.0; rv:17.0) Gecko/20130307 Thunderbird/17.0.4
MIME-Version 1.0
To python-list@python.org
Subject Re: how does the % work?
References <mailman.3612.1363971987.2939.python-list@python.org> <7qbqk8p64dp4v2qvgfogvigi91gjdt5k3r@4ax.com> <514d5bef$0$30001$c3e8da3$5496439d@news.astraweb.com>
In-Reply-To <514d5bef$0$30001$c3e8da3$5496439d@news.astraweb.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
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.3649.1364058458.2939.python-list@python.org> (permalink)
Lines 52
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1364058458 news.xs4all.nl 6915 [2001:888:2000:d::a6]:37211
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:41747

Show key headers only | View raw


thank you all!


Il 23/03/2013 8.38, Steven D'Aprano ha scritto:
> On Fri, 22 Mar 2013 21:29:48 -0700, Tim Roberts wrote:
>
>> leonardo selmi <l.selmi@icloud.com> wrote:
>>> i wrote this example :
>>>
>>> name = raw_input("What is your name?")
>>> quest = raw_input("What is your quest?")
>>> color = raw_input("What is your favorite color?")
>>>
>>> print """Ah, so your name is %s, your quest is %s, and your favorite
>>> color is %s."""  % (name, quest, color)
>> No, you didn't.  You wrote:
>>
>> print('''Ah, so your name is %s, your quest is %s, and your
>>      favorite color is %s.''') % (name, quest, color)
>
> The difference between those two statements may not be entirely clear to
> someone not experienced in reading code carefully.
>
> Consider the difference between:
>
>    print(a % b)
>
>    print(a) % b
>
> In the first example, the round brackets group the "a % b", which is
> calculated first, then printed.
>
> In the second example, in Python 3, the "print(a)" is called first, which
> returns None, and then "None % b" is calculated, which raises an
> exception.
>
> Just to add confusion, the two lines are exactly the same in Python 2,
> where Print is not a function!
>
>
>> You are using Python 3.  In Python 3, "print" is a function that returns
>> None.  So, the error is exactly correct.  To fix it, you need to have
>> the % operator operate on the string, not on the result of the "print"
>> function:
>>
>> print('''Ah, so your name is %s, your quest is %s, and your
>>      favorite color is %s.''' % (name, quest, color))
> Exactly correct.
>
>
>

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

how does the % work? leonardo selmi <l.selmi@icloud.com> - 2013-03-22 18:06 +0100
  Re: how does the % work? John Gordon <gordon@panix.com> - 2013-03-22 17:14 +0000
  Re: how does the % work? Rick Johnson <rantingrickjohnson@gmail.com> - 2013-03-22 11:06 -0700
  Re: how does the % work? Tim Roberts <timr@probo.com> - 2013-03-22 21:29 -0700
    Re: how does the % work? Rick Johnson <rantingrickjohnson@gmail.com> - 2013-03-23 00:04 -0700
    Re: how does the % work? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-23 07:38 +0000
      Re: how does the % work? Rick Johnson <rantingrickjohnson@gmail.com> - 2013-03-23 00:57 -0700
      Re: how does the % work? Michael Torrie <torriem@gmail.com> - 2013-03-23 09:57 -0600
        Re: how does the % work? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-23 23:53 +0000
      Re: how does the % work? leonardo <tampucciolina@libero.it> - 2013-03-23 18:05 +0100
        Re: how does the % work? Tim Roberts <timr@probo.com> - 2013-03-24 16:35 -0700

csiph-web