What is html.unescape() in Python?

HTML code is often encoded when it is saved in a database or a variable. Encoding replaces special HTML reserved characters with their respective entity names or entity numbers defined in the HTML5. Below is a table of HTML reserved special characters and their respective entity name and number:

Character

Entity name

Entity number

>

>

>

<

&lt;

&#60;

"

&quot;

&#34;

&

&amp;

&#38;

The html.escape() method in Python is used to encode HTML. In order to display a web page, we must first decode the encoded HTML so that we can retrieve the original code from the database or variable. Decoding can be done through the html.unescape() method. html.unescape() replaces the entity names or entity numbers of the reserved HTML characters with its original character representation. For example, the string &lt;div\&gt; will be decoded to <div>.

Example

import html
myHtml = "<body><h1> How to use html.unescape() in Python </h1></body>"
encodedHtml = html.escape(myHtml)
print("Encoded HTML: ", encodedHtml)
decodedHtml = html.unescape(encodedHtml)
print("Decoded HTML: ", decodedHtml)

First, import the html module. Pass your encoded HTML string to the html.unescape() function, and it will return the decoded HTML script.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved