Stripping new lines from strings?

Richard Chamberlain richard_chamberlain at ntlworld.com
Tue Aug 15 03:11:02 EDT 2000


Hi Chris,

Presuming you don't want any space at the end you can use string.strip
so...

import string
p="I line with a newline.\n"
q=string.rstrip(p)

or you could use string.replace

q=string.replace(p,'\n','')

so that would replace the newline character(s) with nothing

Richard

CHRIS wrote:
> 
> What is the best (fastest) way to strip new line characters from a
> string in Python? In C I would do:
> 
> char *p;
> p = strchr(str, '\n');
> if(p)
>   *p = '\0';
> 
> /* repeat process for '\r' character */
> 
> Thanks.



More information about the Python-list mailing list