Python Escape Characters In HTML Escaped String Examples

What are escape characters? In HTML, <, >, & and some other characters have special meanings (<, > used in tags and & used for escape). They cannot be used directly in HTML code. If you want to display these symbols on web pages, you need to use HTML escape sequence. For example, the escape character of < is &lt; , when the browser renders an HTML page, it will automatically replace the escape string with a real character.

Escape sequence consists of three parts: the first part is a & symbol, the second part is the entity name, and the third part is a semicolon. For example, to display the less-than sign (<), you should write &lt;.

Below is a list of display character and escape character mapping examples.

Display Html Character Escape Html Character
< &lt;
> &gt;
& &amp;

1. Python Escape Characters In Html Escaped String Examples.

There are many ways to use Python escape characters that have been escaped in Html string, and they are handled differently in Py2 and Py3.

In python2, the unescape module is HTMLParser.

# Python2
import HTMLParser
# The HTMLParser has a unescape() method.
>>> HTMLParser().unescape('username=python&amp;password=escape')
'username=python&password=escape'

The Python3 HTMLParser module was migrated to html.parser.

# Python3
>>> from html.parser import HTMLParser
>>> HTMLParser().unescape('username=python&amp;password=escape')
'username=python&password=escape'

After Python 3.4, the unescape method is added to the html module.

# Python3.4
>>> import html
>>> html.unescape('username=python&amp;password=escape')
'username=python&password=escape'

The last method is recommended, because HTMLParser.unescape() method was deprecated in Python3.4 and is not recommended.

In addition, xml.sax module also has functions that support unescape like below example.

>>> from xml.sax.saxutils import unescape
>>> unescape('username=python&amp;password=escape')
'username=pythin&password=escape'

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.