Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'argument': 0.04; 'essentially': 0.04; 'function,': 0.07; 'valueerror:': 0.07; 'python': 0.09; 'accepts': 0.09; 'arguments,': 0.09; 'function:': 0.09; 'subject:not': 0.11; '(the': 0.15; '(but': 0.15; '2.7.2': 0.16; 'attributes.': 0.16; 'clunky': 0.16; 'concise,': 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; 'tuples,': 0.16; 'string': 0.17; 'wrote:': 0.17; 'specify': 0.17; '>>>': 0.18; 'windows': 0.19; 'bit': 0.21; '"",': 0.22; '(this': 0.24; 'idea': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; '(most': 0.27; 'format,': 0.27; 'run': 0.28; '>>>>': 0.29; 'josh': 0.29; 'received:192.168.1.3': 0.29; "i'm": 0.29; 'usually': 0.30; 'file': 0.32; 'received:84': 0.32; 'running': 0.32; 'print': 0.32; 'traceback': 0.33; 'to:addr :python-list': 0.33; "can't": 0.34; 'list': 0.35; 'there': 0.35; 'method': 0.36; 'why': 0.37; 'subject:: ': 0.38; 'gives': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'skip:" 10': 0.40; 'received:192.168': 0.40; 'more': 0.63; '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; '***': 0.93; '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=iuHaYOYsRq_FDiTaGZUA:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Fri, 29 Jun 2012 18:02:45 +0100 From: MRAB 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> In-Reply-To: <7d1714cb-7aea-4048-bdc5-1b65d0f6c109@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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1340989365 news.xs4all.nl 6860 [2001:888:2000:d::a6]:47128 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:24666 On 29/06/2012 17:31, Josh English wrote: > I have a list of tuples, and usually print them using: > > print c, " ".join(map(str, list_of_tuples)) > > This is beginning to feel clunky (but gives me essentially what I want), and I thought there was a better, more concise, way to achieve this, so I explored the new string format and format() function: > >>>> c = (1,3) >>>> s = "{0[0]}" >>>> print s.format(c) > '1' >>>> print format(c,s) > Traceback (most recent call last): > File "", line 1, in > ValueError: Invalid conversion specification > > I'm running *** Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32. *** > (This is actually a PortablePython run on a Windows 7 machine) > > Any idea why one form works and the other doesn't? > 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'