Dnia Wed, 13 Aug 2008 09:39:53 -0700 (PDT), gjhames napisa�(a): > I wish to replace several characters in my string to only one. > Example, "-", "." and "/" to nothing "" > I did like that: > my_string = my_string.replace("-", "").replace(".", "").replace("/", > "").replace(")", "").replace("(", "") > > But I think it's a ugly way. > > What's the better way to do it?
The regular expression is probably the best way to do it, but if you really want to use replace, you can also use the replace method in loop: >>> somestr = "Qwe.Asd/Zxc()Poi-Lkj" >>> for i in '-./()': ... somestr = somestr.replace(i, '') ... >>> somestr 'QweAsdZxcPoiLkj' >>> Next step would be to define your own replacing function: def my_replace(mystr, mychars, myrepl): """Replace every character from 'mychars' string with 'myrepl' string in 'mystr' string. Example: my_replace('Qwe.Asd/Zxc(', './(', 'XY') -> 'QweXYAsdXYZxcXY'""" for i in mychars: mystr = mystr.replace(i, myrepl) return mystr -- Regards, Wojtek Walczak, http://www.stud.umk.pl/~wojtekwa/
-- http://mail.python.org/mailman/listinfo/python-list