[Tutor] capitalize() but only first letter

Jeff Shannon jeff@ccvcorp.com
Wed Feb 5 22:32:02 2003


Erik Price wrote:

> 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.) 


Yes, this is indeed the reason.  Each addition creates a new string 
object, of which all but one are almost immediately thrown away. 
 Appending to a list and then using join() creates a single string 
object (and may require creating a new list object) regardless of how 
many pieces you're concatenating.

By the way, instead of adding spaces to the list and joining with an 
empty string, I'd simply join on a space.  I also prefer using join() as 
a string method, instead of importing the string module.

l.append('hello')
l.append('world')
' '.join(l)

I suspect that Sean finds the string method syntax of join() to be 
awkward (there was quite a bit of discussion when this was added to 
Python, and many people still disagree with Guido's decision), but I 
find it less awkward than needlessly importing the string module.  ;)

> Finally, how does this compare:
>
> print("%s %s") % ('hello', 'world')


This is much preferable, at least to my tastes.  It still avoids the 
creating of multiple intermediate strings, and also avoids the multiple 
function calls used by the append()/join() method, and is clearer in its 
intent.  There are only three situations where I find the 
append()/join() method preferable to this --

1) The strings are already in a list, so it's really just a single join()
2) There's an extremely large number of strings, so that typing a 
sequence of %s's becomes cumbersome
3) There's a variable number of strings, so that you don't know at 
compile-time just how many %s's to use

Jeff Shannon
Technician/Programmer
Credit International