Python Forum
how to replace comma with string "and" but with conditons - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: how to replace comma with string "and" but with conditons (/thread-12877.html)



how to replace comma with string "and" but with conditons - Prince_Bhatia - Sep-17-2018

Hi,

I have a text written like this :
apartments, villa, studio apartments, shops

i want to replace last comma with "and"

expected output:
apartments, villa, studio apartments and shops

how can i do this?i tried this:
m = apartments, villa, studio apartments, shops

getreplaced = m.replace(","," and ")
but dont know what to do next


RE: how to replace comma with string "and" but with conditons - volcano63 - Sep-17-2018

Split from right by comma and join by and
'and'.join(m.split(',', 1))



RE: how to replace comma with string "and" but with conditons - Prince_Bhatia - Sep-17-2018

i received this result:

apartmrentsandvilla,studio apatments,shops
i used :
mc = 'and'.join(m.split(',', 1))
expected output:
apartments, villa, studio apartments and shops


RE: how to replace comma with string "and" but with conditons - volcano63 - Sep-17-2018

(Sep-17-2018, 11:09 AM)Prince_Bhatia Wrote: i received this result:

apartmrentsandvilla,studio apatments,shops
i used :
mc = 'and'.join(m.split(',', 1))
expected output:
apartments, villa, studio apartments and shops

Sorry, replace split with rsplit