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


Groups > comp.lang.python > #4859

Re: string formatting

References (2 earlier) <4dc3b54c$0$29991$c3e8da3$5496439d@news.astraweb.com> <mailman.1230.1304673808.9059.python-list@python.org> <4dc404b7$0$29991$c3e8da3$5496439d@news.astraweb.com> <RTXwp.18780$Ot6.17862@newsfe15.iad> <D7Ywp.59690$0s5.13933@newsfe17.iad>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2011-05-06 13:54 -0600
Subject Re: string formatting
Newsgroups comp.lang.python
Message-ID <mailman.1263.1304711723.9059.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, May 6, 2011 at 1:39 PM, harrismh777 <harrismh777@charter.net> wrote:
> harrismh777 wrote:    OP wrote:
>
>> (1) "the %s is %s" % ('sky', 'blue')
>>
>> (2) "the {0} is {1}".format('sky', 'blue')
>>
>> (3) "the {} is {}".format('sky', 'blue')
>
>   On the other hand, consider this 3.x code snip:
>
>   print("the %s is %d" % ('sky', 'blue'))
>
>
>   That formatting will throw an exception, because the format construct is
> restricting the format entry to be a number, which 'blue' clearly isn't....

If you used %d, then that exception is presumably what you wanted.  If
not, then you should just do:

print("the %s is %s" % ('sky', 'blue'))

>   The following print() is better, because *any* time or *most* types can be
> substituted and the 'polymorphism' of Python kicks in allowing for that, as
> so:

'%s' has exactly the same degree of polymorphism as '{}', because both
simply call str() on their argument.  There are good reasons to use
format instead of % if possible, but polymorphism isn't one of them.

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


Thread

string formatting Jabba Laci <jabba.laci@gmail.com> - 2011-05-06 03:18 -0400
  Re: string formatting Web Dreamer <webdreamer@nospam.fr> - 2011-05-06 10:00 +0200
    Re: string formatting Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-06 08:46 +0000
      Re: string formatting Chris Rebert <clp2@rebertia.com> - 2011-05-06 02:23 -0700
        Re: string formatting Web Dreamer <webdreamer@nospam.fr> - 2011-05-06 14:10 +0200
          Re: string formatting nn <pruebauno@latinmail.com> - 2011-05-06 06:06 -0700
          Re: string formatting Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-06 14:33 +0000
          Re: string formatting Web Dreamer <webdreamer@nospam.fr> - 2011-05-10 16:15 +0200
            Re: string formatting Chris Angelico <rosuav@gmail.com> - 2011-05-11 00:43 +1000
        Re: string formatting Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-06 14:24 +0000
          Re: string formatting harrismh777 <harrismh777@charter.net> - 2011-05-06 14:22 -0500
            Re: string formatting harrismh777 <harrismh777@charter.net> - 2011-05-06 14:39 -0500
              Re: string formatting Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-06 13:54 -0600
              Re: string formatting Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-07 02:30 +0000
              Re: string formatting alex23 <wuwei23@gmail.com> - 2011-05-08 22:47 -0700
            Re: string formatting Neil Cerutti <neilc@norwich.edu> - 2011-05-06 19:54 +0000
            Re: string formatting Terry Reedy <tjreedy@udel.edu> - 2011-05-06 16:54 -0400
            Re: string formatting Chris Angelico <rosuav@gmail.com> - 2011-05-07 10:09 +1000
            Re: string formatting Terry Reedy <tjreedy@udel.edu> - 2011-05-07 03:55 -0400
  Re: string formatting Raymond Hettinger <python@rcn.com> - 2011-05-10 10:20 -0700

csiph-web