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


Groups > comp.lang.python > #24664

format() not behaving as expected

From Josh English <Joshua.R.English@gmail.com>
Newsgroups comp.lang.python
Subject format() not behaving as expected
Date 2012-06-29 09:31 -0700
Organization http://groups.google.com
Message-ID <7d1714cb-7aea-4048-bdc5-1b65d0f6c109@googlegroups.com> (permalink)

Show all headers | View raw


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 "<interactive input>", line 1, in <module>
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? 

Back to comp.lang.python | Previous | NextNext 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