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

R

Ric Da Force

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
 
W

Wolfram Kraus

Ric said:
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
 
P

Peter Otten

Ric said:
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.
'C1, C2 and C3'

Peter
 
C

Christoph Rackwitz

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 has, try this (i shortened
the output a bit):['__add__', '__doc__', ..., 'capitalize', 'center', 'count',
'decode','encode', 'endswith', 'expandtabs', 'find', 'index',
'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle',
'isupper', 'join', 'ljust', 'lower', 'lstrip', 'replace', 'rfind',
'rindex', 'rjust', 'rsplit', 'rstrip', 'split', 'splitlines',
'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper',
'zfill']
 
B

Brian van den Broek

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
 
R

Ric Da Force

Hi guys,

Thank you all for your input! It was good to see so much convergence in the
approach! Again, I think that it speaks loudly for the concise way of doing
thins in Python... Anyway, I have typed in all of the solutions and have
gained a great understanding of how to do this in future.

Thanks again!

Ric
Brian van den Broek said:
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
 
B

Bengt Richter

Hi guys,

Thank you all for your input! It was good to see so much convergence in the
approach! Again, I think that it speaks loudly for the concise way of doing
thins in Python... Anyway, I have typed in all of the solutions and have
gained a great understanding of how to do this in future.

Thanks again!

Ric
Brian van den Broek said:
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
Or, to finish Brian's solution by inserting the ", and" in place of the "," :
'C1, C2, and C3'

Regards,
Bengt Richter
 
A

Alan Green

Ric said:
Hi guys,

Thank you all for your input! It was good to see so much convergence in the
approach!

Just for divergence, you can also do this with regular expressions:
'C1, C2 and C3'

Alan.
 
F

Florian Diesch

Ric Da Force said:
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.


Florian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top