format() not behaving as expected
Josh English
Joshua.R.English at gmail.com
Fri Jun 29 13:24:58 EDT 2012
On Friday, June 29, 2012 10:08:20 AM UTC-7, Steven D'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
More information about the Python-list
mailing list