Printing formatted strings from a dictionary

Peter Abel PeterAbel at gmx.net
Thu May 20 16:24:05 EDT 2004


Josiah Carlson <jcarlson at uci.edu> wrote in message news:<c8gfov$9j9$1 at news.service.uci.edu>...
> [snip good reply from Peter]
> 
> The other thing to remember is that there are implicit quotes:
>  >>> d = {1:2}
>  >>> "%(1)i"%d
> Traceback (most recent call last):
>    File "<stdin>", line 1, in ?
> KeyError: 1
>  >>> "%(1)i"%d
> '2'

Uuuaaah, I can't stop learning YOU(python). Couldn't verify
the above example. Maybe some fault happened to you because the code
which raised the KeyError and the successfull one are the same for me.
But the following works:
>>> d={'1':2}   # Remark the quotmarks '1'
>>> '%(1)i'%d
'2'
>>> 

I even would not have believed that this kind of formatting would work.
But the more I think about it the more it becomes consequential and logical.
- Peter -

>   - Josiah



More information about the Python-list mailing list