Why does this (not) work?

Anders J. Munch andersjm at dancontrol.dk
Wed Aug 20 09:31:49 EDT 2003


"Cliff Wells" <logiplex at qwest.net> wrote:
> Here's another approach:
> 
> >>> def fjoin(sep, s, n):
> ...     return sep.join(["%s"] * n) % ((s,) * n)

Better this:

    def fjoin(sep, s, n):
        return sep.join((s,)*n)

Works even if sep contains a % character.

>>> fjoin(' % ', "test", 3) 
'test % test % test'

- Anders






More information about the Python-list mailing list