Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #82008 > unrolled thread
| Started by | gaurangnshah@gmail.com |
|---|---|
| First post | 2014-12-05 09:31 -0800 |
| Last post | 2014-12-05 11:14 -0700 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
concat string and dict in print statement gaurangnshah@gmail.com - 2014-12-05 09:31 -0800
Re: concat string and dict in print statement Peter Otten <__peter__@web.de> - 2014-12-05 19:11 +0100
Re: concat string and dict in print statement Ian Kelly <ian.g.kelly@gmail.com> - 2014-12-05 11:14 -0700
| From | gaurangnshah@gmail.com |
|---|---|
| Date | 2014-12-05 09:31 -0800 |
| Subject | concat string and dict in print statement |
| Message-ID | <14b86f3a-7284-4e83-b5c0-1d41c86d80a7@googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2014-12-05 19:11 +0100 |
| Message-ID | <mailman.16633.1417803096.18130.python-list@python.org> |
| In reply to | #82008 |
gaurangnshah@gmail.com wrote:
> 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
Try the str.format() method:
>>> stats = {'lname': 'shah', 'fname': 'gaurang'}
>>> a = "test"
>>> print "{0} {1[fname]} {1[lname]}".format(a, stats)
test gaurang shah
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2014-12-05 11:14 -0700 |
| Message-ID | <mailman.16634.1417803306.18130.python-list@python.org> |
| In reply to | #82008 |
[Multipart message — attachments visible in raw view] — view raw
On Fri, Dec 5, 2014 at 10:31 AM, <gaurangnshah@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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web