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


Groups > comp.lang.python > #24664 > unrolled thread

format() not behaving as expected

Started byJosh English <Joshua.R.English@gmail.com>
First post2012-06-29 09:31 -0700
Last post2012-06-29 10:24 -0700
Articles 7 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  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

#24664 — format() not behaving as expected

FromJosh English <Joshua.R.English@gmail.com>
Date2012-06-29 09:31 -0700
Subjectformat() not behaving as expected
Message-ID<7d1714cb-7aea-4048-bdc5-1b65d0f6c109@googlegroups.com>
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? 

[toc] | [next] | [standalone]


#24666

FromMRAB <python@mrabarnett.plus.com>
Date2012-06-29 18:02 +0100
Message-ID<mailman.1636.1340989364.4697.python-list@python.org>
In reply to#24664
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 "<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?
>
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'

[toc] | [prev] | [next] | [standalone]


#24668

FromJosh English <Joshua.R.English@gmail.com>
Date2012-06-29 10:19 -0700
Message-ID<cb94c214-3bab-49b6-9dd6-c53db060b9dd@googlegroups.com>
In reply to#24666
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? 

Josh

[toc] | [prev] | [next] | [standalone]


#24671

FromMRAB <python@mrabarnett.plus.com>
Date2012-06-29 18:41 +0100
Message-ID<mailman.1638.1340991688.4697.python-list@python.org>
In reply to#24668
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

[toc] | [prev] | [next] | [standalone]


#24669

FromJosh English <Joshua.R.English@gmail.com>
Date2012-06-29 10:19 -0700
Message-ID<mailman.1637.1340990373.4697.python-list@python.org>
In reply to#24666
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? 

Josh

[toc] | [prev] | [next] | [standalone]


#24667

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-06-29 17:08 +0000
Message-ID<4fede104$0$29988$c3e8da3$5496439d@news.astraweb.com>
In reply to#24664
On Fri, 29 Jun 2012 09:31:53 -0700, 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'

That's not actually the output copied and pasted. You have quotes around 
the string, which you don't get if you pass it to the print command.


>>>> print format(c,s)
> Traceback (most recent call last):
>   File "<interactive input>", line 1, in <module>
> ValueError: Invalid conversion specification
[...]
> Any idea why one form works and the other doesn't?

Because the format built-in function is not the same as the format string 
method.

The string method takes brace substitutions, like "{0[0]}" which returns 
the first item of the first argument.

The format function takes a format spec, not a brace substitution. For 
example:

>>> format(c, '<10s')
'(1, 3)    '
>>> format(c, '>10s')
'    (1, 3)'
>>> format(c, '*>10s')
'****(1, 3)'

The details of the format spec are in the Fine Manual:

http://docs.python.org/library/string.html#formatspec

although sadly all the examples are about using brace substitutions, not 
format specs.

(Personally, I find the documentation about format to be less than 
helpful.)

You can also read the PEP that introduced the new formatting, but keep in 
mind that there have been some changes since the PEP was written, so it 
may not quite match the current status quo.

http://www.python.org/dev/peps/pep-3101/


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#24670

FromJosh English <Joshua.R.English@gmail.com>
Date2012-06-29 10:24 -0700
Message-ID<61b63296-d7d5-4c1f-9055-d4e0c3113cb5@googlegroups.com>
In reply to#24667
On Friday, June 29, 2012 10:08:20 AM UTC-7, Steven D&#39;Aprano wrote:
> >>>> c = (1,3)
> >>>> s = "{0[0]}"
> >>>> print s.format(c)
> > '1'
> 
> That's not actually the output copied and pasted. You have quotes around 
> the string, which you don't get if you pass it to the print command.
> 

Mea culpa. I typed it in manually because the direct copy and paste was rather ugly full of errors because of many haplographies.



> >>>> print format(c,s)
> > Traceback (most recent call last):
> >   File "<interactive input>", line 1, in <module>
> > ValueError: Invalid conversion specification
> [...]
> > Any idea why one form works and the other doesn't?
> 
> Because the format built-in function is not the same as the format string 
> method.
...
> 
> (Personally, I find the documentation about format to be less than 
> helpful.)
> 

Thanks. I think it's coming together.

Either way, this format seems uglier than what I had before, and it's longer to type out.  I suppose that's just programmers preference.

Josh

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web