{"id":28008,"date":"2022-05-30T16:32:35","date_gmt":"2022-05-30T11:02:35","guid":{"rendered":"https:\/\/python-programs.com\/?p=28008"},"modified":"2022-05-30T16:32:35","modified_gmt":"2022-05-30T11:02:35","slug":"python-html-escape-function","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-html-escape-function\/","title":{"rendered":"Python html.escape() Function"},"content":{"rendered":"

We must escape special characters that are not markup text but may be misinterpreted as such when saving raw HTML in databases or variables.<\/p>\n

\u00a0<, >, \", ', and &. are examples of these characters.<\/pre>\n

If certain characters are not escaped, the browser may display a web page improperly. For example, the quote marks around “Python Programs” in the following HTML content may cause confusion between the end and beginning of a new string.<\/p>\n

Hello this is \"Python Programs\"<\/pre>\n

Special entity names and entity numbers are essentially escape sequences that replace these characters in HTML. In HTML, escape sequences begin with an ampersand and terminate with a semicolon.<\/p>\n

The table below lists the special characters that HTML 4 recommends escaping, as well as their entity names and numbers:<\/p>\n

Character\u00a0<\/strong> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Entityname\u00a0<\/strong> \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0EntityNumber<\/strong><\/p>\n

>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &gt;\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#62;<\/p>\n

<\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &lt;\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0&#60;<\/p>\n

”\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0&quot;\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0&#34;<\/p>\n

&\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &amp;\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0&#38;<\/p>\n

We can use the html.escape() method in Python to encode your HTML in an ascii string to escape these characters. escape() takes one optional argument quote, which is set to True by default, and an HTML script as an argument. To use html.escape(), you must first import the html module, which is included in Python 3.2 and higher.<\/p>\n

Python html.escape() Function:<\/strong><\/p>\n

Using the html.escape() method, we can convert an html script into a string by replacing special characters with ascii characters.<\/p>\n

Syntax:<\/strong><\/p>\n

 html.escape(string)<\/pre>\n

Return Value:<\/strong><\/p>\n

A string of ASCII character script from HTML is returned by the escape() function.<\/p>\n

html.escape() Function in Python<\/h2>\n

Method #1: Using escape() Function (Static Input)<\/h3>\n

Approach:<\/strong><\/p>\n