Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed1a.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'schema': 0.05; 'string': 0.09; '"("': 0.09; 'driver,': 0.09; 'parameter': 0.09; 'subject:string': 0.09; 'toss': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'stored': 0.12; '"."': 0.16; 'cc:name:python list': 0.16; 'concatenate': 0.16; 'separated': 0.16; 'stuff.': 0.16; 'tuple': 0.16; 'subject:python': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'wed,': 0.18; 'bit': 0.19; 'aug': 0.22; 'cc:addr:python.org': 0.22; 'string,': 0.24; 'fairly': 0.24; 'looks': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'correct': 0.29; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; "skip:' 10": 0.31; 'constant': 0.31; 'parameters.': 0.31; 'text': 0.33; 'skip:s 30': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'thanks': 0.36; 'hi,': 0.36; 'list': 0.37; 'list,': 0.38; 'pm,': 0.38; 'skip:p 20': 0.39; 'called': 0.40; 'new': 0.61; 'simple': 0.61; 'to:addr:gmail.com': 0.65; 'ugly,': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=yVDrGWPtkSxZKSzi24PAsYlfil8WiwM3+h8YXItFaRg=; b=n0Axnb3gYIPlsEDOfW0LnSF1blcsNOU4FbOdB3MG8V9DrN/KEZovmxWZDRjZtaZHXZ b6CjgxnfGyrVuSixp1HnmbQaE58971LAc9Sod2kEAVRhdy1cptnWt79wkVN9owrbk3Wc SOSJp2MyMbIYi13nUC2oM3ksHE1swh9KpMdl1yChCnzozvrUpcAs5ptLULlw5/8sQPss aG96weOzVlZIaV5L31pRbulzoqPgDhEyBfpgyS0nAvat6KvuGQeyotpKp7Q5ZGlbwu6Y CnwHmAzirXAwA6FxWmK4Rxi6pvH86Wkdx4Z4cmG0dqkCNMfCLQqkdvCFsPb6npiSmzAa ye4A== MIME-Version: 1.0 X-Received: by 10.112.160.163 with SMTP id xl3mr3943lbb.80.1409178645166; Wed, 27 Aug 2014 15:30:45 -0700 (PDT) In-Reply-To: <55bab2a0-e0bc-4398-90b4-c9937498f5d8@googlegroups.com> References: <55bab2a0-e0bc-4398-90b4-c9937498f5d8@googlegroups.com> Date: Wed, 27 Aug 2014 15:30:45 -0700 Subject: Re: python string, best way to concat From: Dan Stromberg To: dennisearlevans@gmail.com Content-Type: text/plain; charset=UTF-8 Cc: Python List 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: 58 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1409178652 news.xs4all.nl 2834 [2001:888:2000:d::a6]:53835 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:77166 On Wed, Aug 27, 2014 at 1:31 PM, 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? > > thanks > > Dennis If you have a constant number of things to put in a string, often 'abc {} ghi {}'.format('def', 'jkl') is good - this will produce 'abc def ghi jkl'. If you have a variable number of things to put in a string, toss them all in a list, and join them: list_ = ['abc, 'def', 'ghi'] for number in range(4): list_.append(str(number)) list_.extend(['jkl', 'mno') # elements of the list will be separated by one space: string = ' '.join(list_)