Removing a substring from a string

Peter Hansen peter at
Mon Jan 16 12:51:21 CET 2006

ankit wrote:
> I am using mainstr.replace(substr, "") but it gives me additional
> carriage returns which leads to empty spaces as follows:

The .replace() method does *not* introduce additional carriage returns 
(nor newlines/linefeeds, which is probably what you meant).  If you 
think it does, your tests are flawed or you are misinterpreting what is 

Maybe start with a simpler test case, and make use of repr() on your 
output to let you compare the input and output more accurately.

If you still think newlines are being introduced, please post the 
*actual code* you are using.  Do not just retype it: cut and paste it so 
we know it's reliable.

(I tried with what you posted, and after correcting for the missing 
quotation mark when you defined substr, I get the results you wanted, 
not what you showed.)


More information about the Python-list mailing list