How to print the \n character in Jinja2
<p>I have the following string in Python: <code>thestring = "123\n456"</code></p>

<p>In my Jinja2 template, I use <code>{{thestring}}</code> and the output is:</p>

<blockquote>
  <p>123<br>
  456</p>
</blockquote>

<p>The only way I can get Jinja2 to print the exact representation <code>123\n456</code> (including the <code>\n</code>) is by escaping <code>thestring = "123\\n456"</code>.</p>

<p><strong>Is there any other way this can be done directly in the template?</strong></p>

<p>I'll answer my own, maybe it helps someone who has the same question.</p>

<p>This works: <code>{{thestring.encode('string_escape')}}</code></p>

python flask jinja2