[Tutor] Converting a List/Tuple to delimited string

John Fouhy john at fouhy.net
Mon Nov 14 05:11:16 CET 2005


On 14/11/05, Roy Bleasdale <royb at ubiquity.com.au> wrote:
> I have a list of strings and wanted to output them as a single delimited
> string.
>
> Eg
>
> ('ab','cd','ef') becomes "ab:cd:ef"

You almost had it ...

What about:

>>> lst = ['ab', 'cd', 'ef']
>>> ':'.join(lst)
'ab:cd:ef'

In general, foo.join(lst) (where foo is a string) is equivalent to:

def join(foo, lst):
  if not lst:
      return ''
  s = lst[0]
  for item in lst[1:]:
      s += foo + item
  return s

--
John.


More information about the Tutor mailing list