concat string and dict in print statement

Ian Kelly ian.g.kelly at gmail.com
Fri Dec 5 13:14:17 EST 2014


On Fri, Dec 5, 2014 at 10:31 AM, <gaurangnshah at gmail.com> wrote:
>
> Hi Guys,
>
> I am trying to combine string and dict in the print statement, however
getting an error. Would someone let me know what will be correct way to do
that.
>
> stats={'lname': 'shah', 'fname': 'gaurang'}
> a=test
>
> print "%s %(fname)s %(lname)s" %(a,stats)
>
> Following is the error I am getting
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: format requires a mapping

You need to use either a tuple or a mapping, not a mapping inside a tuple.
Or switch to using str.format, which allows more advanced attribute/item
look-ups like this.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20141205/910d13e6/attachment.html>


More information about the Python-list mailing list