Removing a substring from a string

Peter Hansen peter at engcorp.com
Mon Jan 16 06:51:21 EST 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 
happening.

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.)

-Peter




More information about the Python-list mailing list