Replacing last comma in 'C1, C2, C3' with 'and' so that it reads 'C1, C2 and C3'

Christoph Rackwitz christoph.rackwitz at gmail.com
Tue Jul 12 02:55:46 EDT 2005


foo = "C1, C2, C3"
foo = foo.split(", ") # ['C1', 'C2', 'C3']
foo = ", ".join(foo[:-1]) + " and " + foo[-1] # just slicing and
joining it

you can always look for something here:
http://docs.python.org/lib/lib.html
and there:
http://docs.python.org/ref/

if you want to know, what methods an object has, try this (i shortened
the output a bit):
>>> foo = "C1, C2, C3"
>>> dir(foo)
['__add__', '__doc__', ..., 'capitalize', 'center', 'count',
'decode','encode', 'endswith', 'expandtabs', 'find', 'index',
'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle',
'isupper', 'join', 'ljust', 'lower', 'lstrip', 'replace', 'rfind',
'rindex', 'rjust', 'rsplit', 'rstrip', 'split', 'splitlines',
'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper',
'zfill']
>>>




More information about the Python-list mailing list