How to subtract two strings?

max smith picture max smith · Aug 27, 2013 · Viewed 82.7k times · Source

I have a long string, which is basically a list like str="lamp, bag, mirror," (and other items)

I was wondering if I can add or subtract some items, in other programming languages I can easily do: str=str-"bag," and get str="lamp, mirror," this doesnt work in python (I'm using 2.7 on a W8 pc)

Is there a way to split the string across say "bag," and somehow use that as a subtraction? Then I still need to figure out how to add.

Answer

Joran Beasley picture Joran Beasley · Aug 27, 2013

you could also just do

print "lamp, bag, mirror".replace("bag,","")