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

Brian van den Broek bvande at po-box.mcgill.ca
Tue Jul 12 09:03:44 CEST 2005


Ric Da Force said unto the world upon 12/07/2005 02:43:
> Hi,
> 
> I have a string such as 'C1, C2, C3'.   Without assuming that each bit of 
> text is of fixed size, what is the easiest way to change this list so that 
> it reads:
> 'C1, C2 and C3' regardless of the length of the string.
> 
> Regards and sorry for the newbie question,
> 
> Ric 
> 

Hi Ric,

the rsplit method of strings should get you going:

 >>> data = "the first bit, then the second, finally the third"
 >>> chunks = data.rsplit(',', 1)
 >>> chunks
['the first bit, then the second', ' finally the third']
 >>>

Best,

Brian vdB





More information about the Python-list mailing list