HTML Encode Online

HTML Encoding in Python

In Python, HTML encoding and decoding can be done by using Python standard library. Depending on Python version the library is little different, though.

Python 3

In Python 3, use html.escape() for encoding and html.unescape() for decoding.

import html
encodedText = html.escape('<html></html>')
decodedText = html.unescape(encodedText)

Python 2

In Python 2, use cgi.escape() for encoding and HTMLParser.HTMLParser().unescape() for decoding.

import HTMLParser
encodedText = cgi.escape('<html></html>')
decodedText = HTMLParser.HTMLParser().unescape(encodedText)


In Django, HTML encoding (or escaping) occurs during template rendering. To prevent escaping in Django template, we can ask the template engine not to escape strings. To do that, use either safe template filter or autoescape off in your template as below.

{{ yourtext|safe }}


{% autoescape off %}
    {{ yourtext }}
{% endautoescape %}