[Tutor] capitalize() but only first letter

Erik Price erikprice@mac.com
Wed Feb 5 21:51:04 2003


On Wednesday, February 5, 2003, at 08:25  PM, Sean 'Shaleh' Perry wrote:

> it is generally more efficient to do:
>
> l = []
> l.append('hello')
> l.append(' ')
> l.append('world')
> import string
> output = string.join('', l) # use an empty string as glue
>
> than it is to use 'hello' + ' ' + 'world', especially when the number 
> of
> pieces glued together is larger than a handful.

Is that because concatenating separate strings instantiates separate 
objects, whereas appending to a list does not?  (There is a similar 
though not identical condition in Java with string concatenation vs 
StringBuffer, but the rules may be different in Python.)

If my guess is wrong, what is the reason for the better efficiency in 
appending to a list?

Finally, how does this compare:

print("%s %s") % ('hello', 'world')



TIA,

Erik




-- 
Erik Price

email: erikprice@mac.com
jabber: erikprice@jabber.org