Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'string': 0.09; 'method:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:string': 0.09; '"test"': 0.16; 'dict': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'stats': 0.16; 'str.format()': 0.16; 'typeerror:': 0.16; 'wrote:': 0.18; 'trying': 0.19; '>>>': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'skip:{ 20': 0.24; 'header:X-Complaints-To:1': 0.27; 'correct': 0.29; 'getting': 0.31; 'that.': 0.31; '"",': 0.31; 'file': 0.32; '(most': 0.33; 'test': 0.35; 'error.': 0.37; 'mapping': 0.38; 'to:addr:python- list': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'email addr:gmail.com': 0.63; 'statement,': 0.68; 'shah': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: concat string and dict in print statement Date: Fri, 05 Dec 2014 19:11:21 +0100 Organization: None References: <14b86f3a-7284-4e83-b5c0-1d41c86d80a7@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bda6b5.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 24 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1417803096 news.xs4all.nl 2970 [2001:888:2000:d::a6]:33076 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:82012 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 "", line 1, in > 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