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

Cyril Bazin cyril.bazin at gmail.com
Tue Jul 12 03:21:08 EDT 2005


If that can help you...

def replaceLastComma(s):
i = s.rindex(",")
return ' and'.join([s[:i], s[i+1:]])

I don't know of ot's better to do a:
' and'.join([s[:i], s[i+1:]])
Or:
''.join([s[:i], ' and', s[i+1:]])
Or:
s[:i] + ' and' + s[i+1]
Maybe the better solution is not in the list...

Cyril


On 7/12/05, Ric Da Force <ric at next-level.com.au> 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
> 
> 
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20050712/3196a3b2/attachment.html>


More information about the Python-list mailing list