Trusted answers to developer questions

What is html.unescape() in Python?

Get Started With Data Science

Learn the fundamentals of Data Science with this free course. Future-proof your career by adding Data Science skills to your toolkit — or prepare to land a job in AI, Machine Learning, or Data Analysis.

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.

RELATED TAGS

html
python

CONTRIBUTOR

Abdul Monum
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?