Re: [Tutor] append string
somestring = ABC somestring2 = somestring + D somestring2 += EF assert somestring2 == ABCDEF assert somestring == ABC assert id(somestring) != id(somestring2) Basically, strings are immutable. If you need to append something to a string, you need to construct a new string object with the new value. Now if you are using this to collect huge outputfiles in pieces, one of the common idioms in Python is collecting it in a list, and converting to string at the end: collector = [] for i in xrange(10): collector.append((str(i) * 80)[0:80]) string = .join(collector) assert len(string) == 800 # ~8MB Andreas Am Freitag, den 21.03.2008, 17:04 -0700 schrieb elis aeris: how do I append to the end of strings? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor signature.asc Description: Dies ist ein digital signierter Nachrichtenteil ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] append string
On Sat, Mar 22, 2008 at 09:46:58AM +0100, Andreas Kostyrka wrote: Basically, strings are immutable. If you need to append something to a string, you need to construct a new string object with the new value. Now if you are using this to collect huge outputfiles in pieces, one of the common idioms in Python is collecting it in a list, and converting to string at the end: collector = [] for i in xrange(10): collector.append((str(i) * 80)[0:80]) string = .join(collector) assert len(string) == 800 # ~8MB That was formerly good advice for sure, and it probably still is good advice. But, read the following note from http://docs.python.org/lib/typesseq.html: (6) If s and t are both strings, some Python implementations such as CPython can usually perform an in-place optimization for assignments of the form s=s+t or s+=t. When applicable, this optimization makes quadratic run-time much less likely. This optimization is both version and implementation dependent. For performance sensitive code, it is preferable to use the str.join() method which assures consistent linear concatenation performance across versions and implementations. Changed in version 2.4: Formerly, string concatenation never occurred in-place. As the above note says, this optimization is implementation dependent. In particular, if you plan on moving your code to Jython, then follow Andreas's suggestion to use list append followed by string join. - Dave -- Dave Kuhlman http://www.rexx.com/~dkuhlman ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] append string
Strings are immutable, you can't append to them. How about this mystring = 'Elis' mystring.append('Aeris') Traceback (most recent call last): File stdin, line 1, in module AttributeError: 'str' object has no attribute 'append' mystring + ' Aeris' 'Elis Aeris' x = mystring + ' Aeris' print x Elis Aeris On Fri, Mar 21, 2008 at 8:04 PM, elis aeris [EMAIL PROTECTED] wrote: how do I append to the end of strings? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor