Python Forum

Full Version: how to replace comma with string "and" but with conditons
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
Split from right by comma and join by and
'and'.join(m.split(',', 1))
i received this result:

apartmrentsandvilla,studio apatments,shops
i used :
mc = 'and'.join(m.split(',', 1))
expected output:
apartments, villa, studio apartments and shops
(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