Replacing last comma in 'C1, C2, C3' with 'and' so that it reads 'C1, C2 and C3'
Wolfram Kraus
kraus at hagen-partner.de
Tue Jul 12 02:50:33 EDT 2005
Ric Da Force wrote:
> 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
>
>
Use rfind and slicing:
>>> x = "C1, C2, C3"
>>> x[:x.rfind(',')]+' and'+x[x.rfind(',')+1:]
'C1, C2 and C3'
HTH,
Wolfram
More information about the Python-list
mailing list