[Tutor] Noob question
Amit Saxena
amitsaxena69 at gmail.com
Tue Dec 11 05:32:37 CET 2007
Hi Eric,
Thanks for the information..............since i m new to Python so didnt
knew about this fact...........thanks again for it, will keep it in mind
while writing such a program.
Amit
On Dec 10, 2007 8:57 PM, Eric Brunson <brunson at brunson.com> wrote:
>
> Hi Amit,
>
> This is fairly inefficient. Because strings in python are immutable
> this approach causes a new string to be created every iteration. While
> it may not be an issue with just 3 strings, it is much better to create
> your list and use "".join() to create the concatenation after the list
> is complete. When you get used to it, it's a bit more concise and
> readable, also.
>
> Sincerely,
> e.
>
> Amit Saxena wrote:
> > The simplest way i could think of:
> >
> > a=["apple","orange","banana"]
> > b = ""
> > for i in range(len(a)):
> > b += a[i]
> > print b
> >
> >
> > Amit
> >
> > On Dec 10, 2007 6:48 AM, Alan Gauld <alan.gauld at btinternet.com
> > <mailto:alan.gauld at btinternet.com>> wrote:
> >
> > "Eric Walstad" <eric at ericwalstad.com
> > <mailto:eric at ericwalstad.com>> wrote
> >
> > > You could also achieve the same result of concatenating a list of
> > > strings by looping over the list items like so:
> > >
> > > b = ''
> > > for fruit in a:
> > > b += fruit
> > >
> > > print b
> >
> > And to add to the options you could use the formatting operator
> > provided you know there are only 3 items,
> >
> > b = "%s%s%s" % tuple(a)
> >
> > Or for an indefinite number of strings:
> >
> > b = "%s" * len(a)
> > b = b % tuple(a)
> >
> > So many options. However, to the OP, if you get stuck in
> > future its best if you post the erroneous code that you have tried,
> > then we can better see where you have gone wrong and thus
> > provide clearer guidance on how to fix it. Its better to learn to
> > do it your own way correctly than just to see other folks
> > attempts .
> >
> > HTH,
> >
> > --
> > Alan Gauld
> > Author of the Learn to Program web site
> > http://www.freenetpages.co.uk/hp/alan.gauld
> >
> > _______________________________________________
> > Tutor maillist - Tutor at python.org <mailto:Tutor at python.org>
> > http://mail.python.org/mailman/listinfo/tutor
> >
> >
> >
> > ------------------------------------------------------------------------
> >
> > _______________________________________________
> > Tutor maillist - Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
>
>
--
Thanks and Regards,
Amit Saxena
Senior QA Engineer
Ketera
Direct: +91 4199 5028
Mobile: +91 99001 18641
asaxena at ketera.com
» Visit us at http://www.ketera.com
» Watch the demo at http://www.ketera.com/resources/demos.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20071211/530f8c90/attachment.htm
More information about the Tutor
mailing list