[Python-Dev] String methods... finally
M.-A. Lemburg
mal at lemburg.com
Fri Jun 11 10:15:33 CEST 1999
David Ascher wrote:
>
> On Fri, 11 Jun 1999, Guido van Rossum wrote:
>
> > Perhaps join() ought to be a built-in function?
>
> Would it do the moral equivalent of a reduce(operator.add, ...) or of a
> string.join?
>
> I think it should do the former (otherwise something about 'string' should
> be in the name), and as a consequence I think it shouldn't have the
> default whitespace spacer.
AFAIK, Guido himself proposed something like this on c.l.p a
few months ago. I think something like the following written
in C and optimized for lists of strings might be useful:
def join(sequence,sep=None):
x = sequence[0]
if sep:
for y in sequence[1:]:
x = x + sep + y
else:
for y in sequence[1:]:
x = x + y
return x
>>> join(('a','b'))
'ab'
>>> join(('a','b'),' ')
'a b'
>>> join((1,2,3),3)
12
>>> join(((1,2),(3,)))
(1, 2, 3)
Also, while we're at string functions/methods. Some of the stuff
in mxTextTools (see Python Pages link below) might be of general
use as well, e.g. splitat(), splitlines() and charsplit().
--
Marc-Andre Lemburg
______________________________________________________________________
Y2000: 203 days left
Business: http://www.lemburg.com/
Python Pages: http://www.lemburg.com/python/
More information about the Python-Dev
mailing list