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

Florian Diesch diesch at spamfence.net
Wed Jul 13 10:21:32 EDT 2005


Ric Da Force <ric at next-level.com.au> wrote:
> 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.

>>> import re
>>> data = "the first bit, then the second, finally the third"
>>> re.sub(r"(.*),", r"\1 and", data)
'the first bit, then the second and finally the third'
>>> 


   Florian
-- 
begin  signature_virus
 Hi! I'm a signature virus. Please copy me to your signature to help me spread.
end



More information about the Python-list mailing list