formatting list -> comma separated (slightly different)

egbert egbert.bouwman at hccnet.nl
Wed Jul 9 17:08:22 EDT 2008


On Wed, Jul 09, 2008 at 10:25:33PM +0200, Michiel Overtoom wrote:
> Formatting a sequence of items such that they are separated by
> commas, except the last item, which is separated by the word 'and'.
> 
> For example:
> 
> Three friends have a dinner: Anne, Bob and Chris

row = ["Anne","Bob","Chris"]
txt = ", ".join(row)
pos = txt.rfind(", ")
new = "%s and %s" % (txt[0:pos], txt[pos+2:])

egbert
-- 
Egbert Bouwman - Keizersgracht 197 II - 1016 DS  Amsterdam - 020 6257991
========================================================================



More information about the Python-list mailing list