Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'schema': 0.05; 'conventions': 0.07; 'string': 0.09; '"("': 0.09; 'driver,': 0.09; 'escape': 0.09; 'formatting': 0.09; 'parameter': 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; 'python': 0.11; 'def': 0.12; 'stored': 0.12; '"."': 0.16; '"it\'s': 0.16; 'concatenate': 0.16; 'parts.': 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; 'skip:{ 30': 0.16; 'stuff.': 0.16; 'tuple': 0.16; 'url:pep-0008': 0.16; 'url:peps': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'bit': 0.19; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'skip:{ 20': 0.24; 'url:dev': 0.24; 'fairly': 0.24; 'looks': 0.24; 'question': 0.24; 'skip:" 20': 0.27; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'correct': 0.29; 'code': 0.31; 'parameters.': 0.31; 'text': 0.33; 'url:python': 0.33; 'style': 0.33; 'skip:s 30': 0.35; '(2)': 0.35; 'but': 0.35; 'there': 0.35; 'method': 0.36; 'hi,': 0.36; 'url:org': 0.36; 'two': 0.37; 'list': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'called': 0.40; 'easy': 0.60; 'problems.': 0.60; 'new': 0.61; 'simple': 0.61; 'complete': 0.62; 'email addr:gmail.com': 0.63; 'become': 0.64; 'containing': 0.69; 'adopting': 0.84; 'ugly,': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: python string, best way to concat Date: Thu, 28 Aug 2014 00:34:08 +0200 Organization: None References: <55bab2a0-e0bc-4398-90b4-c9937498f5d8@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd9c3c.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: 79 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1409178864 news.xs4all.nl 2950 [2001:888:2000:d::a6]:56532 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:77167 dennisearlevans@gmail.com wrote: > > Hi, > > Sorry about the simple question but I am very new to Python. > > Anyway, I have a function that will be used to call a stored procedure > and I need to format the string with the correct number of parameter > markers for the ODBC driver, fairly standard stuff. > > What I have works but looks ugly, is there a better way to build or > concatenate a string or is a list or a tuple a better option? > > the function looks like this > > def callSp(self, schema, spName) : > sqlCode = "{call " + schema + "." + spName + "(" > par_Markers = "" > y = len(self.param) > x = 0 > while x < y : > par_Markers = par_Markers.join("?") > if (x < y - 1) : > par_Markers = par_Markers.join(", ") > x += 1 > self.cmdText = sqlCode + par_Markers + ")}" > self.ExecuteCursor() > > return > > self.param is a list of parameters. sself.cmdText is the text that will > be used to call the stored procedure. > > the function would be called like this > > class.AddParameter(some value 1) > class.AddParameter(some value 2) > callSp("schemaName", "storedProcedureName") > > the self.cmdText will look like this just before the ExecuteCursor call > > "{call schemaName.StoredProcedureName(?, ?)}" > > the code works, but as I said is a bit ugly, is there better methods? If I understand you correctly you have two problems. (1) build a string from a few parts. You can use string formatting for that: >>> "It's a {size} {animal}".format(size="big", animal="bird") "It's a big bird" You can escape { and } by repeating them: >>> "{{It's a {size} {animal}}}".format(size="big", animal="bird") "{It's a big bird}" (2) Build a string containing a comma-separated list with a varying number of question marks. That's easy with str.join(): >>> ", ".join(7*"?") '?, ?, ?, ?, ?, ?, ?' The complete method may become def callSp(self, schema, spName) : qm = ", ".join("?"*len(self.param)) self.cmdText = "{{call {schema}.{procname} ({qm})}}".format( schema=schema, procname=spName, qm=qm) self.ExecuteCursor() ... but please consider adopting the conventions recommended in the style guide http://legacy.python.org/dev/peps/pep-0008/ before you write a significant amount of Python code.