Replace n with

Posted on

Question :

Replace n with <br />

I’m parsing text from file with Python. I have to replace all newlines (n) with
cause this text will build html-content. For example, here is some line from file:


Now I do:

thatLine.replace('n', '<br />')
print thatLine

And I still see the text with newline after it.

Asked By: Max Frai


Answer #1:

Just for kicks, you could also do

mytext = "<br />".join(mytext.split("n"))

to replace all newlines in a string with <br />.

Answered By: Tim Pietzcker

Answer #2:

thatLine = thatLine.replace('n', '<br />')

str.replace() returns a copy of the string, it doesn’t modify the string you pass in.

Answered By: Falmarri

Leave a Reply

Your email address will not be published. Required fields are marked *