bug in string.join()?

Adam DePrince adam at deprince.net
Wed Feb 21 14:33:59 EST 2001

luc lam wrote:
> Coffee went everywhere when I read that
>  Daniel Klein <danielk at aracnet.com> had written:
> >On Wed, 21 Feb 2001 20:32:11 +0100, Fernando Rodríguez <spamers at must.die>
> >wrote:
> >>>>> string.join(("abcde"))
> >>'a b c d e'
> >>Is this the expected behavior, or is it a bug? O:-)
> >What would you expect it to do? It's already 'joined' without spaces to start
> >with.
> >Dan
> It threw me somewhat at first:
> >>> import string
> >>> list = ["i've","come","for","an","argument"]
> >>> print string.join(list)
> i've come for an argument
> >>> print string.join(list," ")
> i've come for an argument
> >>> print string.join(list,"")
> i'vecomeforanargument
> so the " " is implicit. i've sometimes thought it would be more intuitive to
> have the "" behaviour as the implicit behaviour, but it's simple enough either

When constructing strings from lists the default value of ' ' as a
delimiter is a pain.  Consider that string.join is often used to replace
string interpolation.  Consider this straw man:

queries = []
for ....
	queries.append("insert into table xyz values '%s','%s','%s' .... 

One might prefer to say 

queries = []
for .....
	queries.append( map( str, ["insert into table xyz values '", d1, "','"

queries = map( string.join, queries )

Of course, for this to work, you would have to say:

queries = map( string.join, queries, ("",)*len(queries))

So, should we propose a join0 that has a delimiter of '', or a version
of map that provides default parameters :-)

Now, to play devils advocate:

' ' makes a convenient delimiter for seperating words in text; it also
matches the defalt delimiter in split.  Notice that:

>>> from string import * 
>>> split( join( ["i've","come","for","an","arguement"]))
["i've", 'come', 'for', 'an', 'arguement']

If join used '' then word lists could not be broken appart and rejoined
so easily.

> way i guess.
> luc

Adam DePrince
Starmedia Network, Inc.

More information about the Python-list mailing list