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


Groups > comp.lang.python > #41747

Re: how does the % work?

Date 2013-03-23 18:05 +0100
From leonardo <tampucciolina@libero.it>
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>
Newsgroups comp.lang.python
Message-ID <mailman.3649.1364058458.2939.python-list@python.org> (permalink)

Show all headers | 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