format() not behaving as expected

J

Josh English

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?
 
M

MRAB

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.
'foo'
 
S

Steven D'Aprano

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.

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:
'****(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/
 
J

Josh English

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.

'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
 
J

Josh English

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.

'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
 
J

Josh English

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
 
M

MRAB

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:
1.23
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top