How can I convert String (with linebreaks) to HTML?

Question:

When I print the string (in Python) coming from a website I scraped it from, it looks like this:

"His this 

is 

a sample

String"

It does not show the n breaks. this is what I see in a Python interpreter.

And I want to convert it to HTML that will add in the line breaks. I was looking around and didn’t see any libraries that do this out of the box.

I was thinking BeautifulSoup, but wasn’t quite sure.

Asked By: Morgan Allen

||

Answers:

If you have a String that you have readed it from a file you can just replace n to <br>, which is a line break in html, by doing:

my_string.replace('n', '<br>')
Answered By: Pablo Martinez

I believe this will work

for line in text:
   for char in line:
      if char == "/n":
         text.replace(char, "<br>")
Answered By: tygzy

You can use the python replace(...) method to replace all line breaks with the html version <br> and possibly surround the string in a paragraph tag <p>...</p>. Let’s say the name of the variable with the text is text:

html = "<p>" + text.replace("n", "<br>") + "</p>"
Answered By: Omari Celestine

searching for this answer in found this, witch is likely better because it encodes all characters, at least for python 3
Python – Convert HTML Characters To Strings

# import html
import html

# Create Text
text = 'Γeeks for Γeeks'

# It Converts given text To String
print(html.unescape(text))

# It Converts given text to HTML Entities
print(html.escape(text))
Answered By: Cedric lacrambe

If you want paragraphs (<p> tags) instead of breaks (<br> tags), you can use a regex:

import re

def text_to_html_paragraphs(text):
    # First, replace multiple newlines with a single newline,
    # so you don't get empty paragraphs
    text = re.sub(r'ns*n', 'n', text)

    # Split the text into lines
    lines = text.split('n')

    # Wrap each line in a <p> tag and join them
    return ''.join(f'<p>{line.strip()}</p>n' for line in lines)

text = """His this 

is 

a sample

String"""

html_paragraphs = text_to_html_paragraphs(text)
print(html_paragraphs)

Result:

<p>is</p>
<p>a sample</p>
<p>String</p>
Answered By: Webucator
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.