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


Groups > comp.lang.python > #24671

Re: format() not behaving as expected

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'argument': 0.04; 'mrab': 0.05; 'function,': 0.07; 'accepts': 0.09; 'arguments,': 0.09; 'braces': 0.09; 'friday,': 0.09; 'subject:not': 0.11; '(the': 0.15; '":"': 0.16; '"to': 0.16; 'attributes.': 0.16; 'braces,': 0.16; 'examples:': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'subscripting': 0.16; 'string': 0.17; 'wrote:': 0.17; 'example.': 0.17; 'skip:{ 20': 0.17; 'specify': 0.17; '>>>': 0.18; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'format,': 0.27; "doesn't": 0.28; 'decimal': 0.29; 'josh': 0.29; 'received:192.168.1.3': 0.29; 'no,': 0.29; 'sense': 0.31; 'received:84': 0.32; 'print': 0.32; '"")': 0.33; 'right?': 0.33; 'to:addr:python-list': 0.33; "can't": 0.34; 'method': 0.36; 'thank': 0.36; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'skip:" 10': 0.40; 'received:192.168': 0.40; 'you.': 0.61; 'between': 0.63; 'more': 0.63; 'here': 0.65; 'therefore': 0.65; 'header:Reply-To:1': 0.68; 'reply-to:no real name:2**0': 0.72; "'foo'": 0.84; 'reply-to:addr:python.org': 0.84; 'hand,': 0.97
X-CM-Score 0.00
X-CNFS-Analysis v=2.0 cv=AYoz7grG c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=YsUzL_8ObRgA:10 a=6IlhpU6xNbwA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=4p39GhNCSa4-_8gMPt8A:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117
X-AUTH mrabarnett:2500
Date Fri, 29 Jun 2012 18:41:29 +0100
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20120614 Thunderbird/13.0.1
MIME-Version 1.0
To python-list@python.org
Subject Re: format() not behaving as expected
References <7d1714cb-7aea-4048-bdc5-1b65d0f6c109@googlegroups.com> <mailman.1636.1340989364.4697.python-list@python.org> <cb94c214-3bab-49b6-9dd6-c53db060b9dd@googlegroups.com>
In-Reply-To <cb94c214-3bab-49b6-9dd6-c53db060b9dd@googlegroups.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.12
Precedence list
Reply-To python-list@python.org
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.1638.1340991688.4697.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1340991688 news.xs4all.nl 6885 [2001:888:2000:d::a6]:41406
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:24671

Show key headers only | View raw


On 29/06/2012 18:19, Josh English wrote:
> On Friday, June 29, 2012 10:02:45 AM UTC-7, MRAB wrote:
>>
>> The ".format" method accepts multiple arguments, so the placeholders in
>> the format string need to specify which argument to format as well as
>> how to format it (the format specification after the ":").
>>
>> The "format" function, on the other hand, accepts only a single
>> argument to format, so it needs only the format specification, and
>> therefore can't accept subscripting or attributes.
>>
>>  >>> c = "foo"
>>  >>> print "{0:s}".format(c)
>> foo
>>  >>> format(c, "s")
>> 'foo'
>
> Thank you. That's beginning to make sense to me. If I understand this,
 > everything between the braces is the format specification,
> and the format specification doesn't include the braces, right?
>
No, the format specification is the part after the ":" (if present), as
in the example.

Here are more examples:

 >>> c = "foo"
 >>> print "{0}".format(c)
foo
 >>> format(c, "")
'foo'
 >>> print "To 2 decimal places: {0:0.2f}".format(1.2345)
To 2 decimal places: 1.23
 >>> print format(1.2345, "0.02f")
1.23

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


Thread

format() not behaving as expected Josh English <Joshua.R.English@gmail.com> - 2012-06-29 09:31 -0700
  Re: format() not behaving as expected MRAB <python@mrabarnett.plus.com> - 2012-06-29 18:02 +0100
    Re: format() not behaving as expected Josh English <Joshua.R.English@gmail.com> - 2012-06-29 10:19 -0700
      Re: format() not behaving as expected MRAB <python@mrabarnett.plus.com> - 2012-06-29 18:41 +0100
    Re: format() not behaving as expected Josh English <Joshua.R.English@gmail.com> - 2012-06-29 10:19 -0700
  Re: format() not behaving as expected Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-06-29 17:08 +0000
    Re: format() not behaving as expected Josh English <Joshua.R.English@gmail.com> - 2012-06-29 10:24 -0700

csiph-web