Ric Da Force <[EMAIL PROTECTED]> 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 fir
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...
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.
>
> Rega
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
Ric Da Force 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.
>>> " and".join("C1, C2, C3".rsplit(",", 1))
'C1, C2 a
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 questio
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.or