Article · 2025-03-27

Understanding and Using Character Entities: From Basics to Practice

Character entities are widely used in markup languages like HTML and XML to represent special symbols, whitespace, and line breaks. By encoding these characters as entities, you ensure that parsers recognize them as content rather than markup syntax, allowing them to display correctly. Common character entities include:

Entity Character Description
< < Less than
> > Greater than
& & Ampersand
" " Double quote
' ' Single quote
  (space) Non-breaking space
© © Copyright symbol
® ® Registered trademark
Trademark
Euro
£ £ Pound sterling
¥ ¥ Yen
¢ ¢ Cent
α α Greek letter alpha
β β Greek letter beta
γ γ Greek letter gamma
ω ω Greek letter omega

The entities listed above are named character entities, using symbolic names such as lt or amp to reference specific characters. Besides named entities, you can also use numeric character entities, which reference a character directly by its Unicode code point. Numeric entities support both decimal format (like &#169;) and hexadecimal format (like &#x00A9;), both representing the copyright symbol ©. When you can't recall a named entity or one doesn't exist, numeric entities provide a universal fallback.

HTML Escaping and Unescaping in Python

With Python 3's adoption of Unicode by default, strings handle most characters directly without encoding issues. However, when processing HTML or XML data in Python, you still need to manage character entity conversions correctly. Web scrapers may retrieve text containing sequences like &lt; or &amp; that require conversion back to their original symbols. Conversely, when generating HTML output, you must convert special symbols into entities to ensure the browser renders them properly.

Python's standard library provides straightforward utilities for HTML entity escaping and unescaping. To escape characters (converting symbols to entities), use html.escape():

import html
text = '5 > 3 & 2 < 4'
escaped_text = html.escape(text)
print(escaped_text)  # 输出: 5 &gt; 3 &amp; 2 &lt; 4

As shown, the characters >, &, and < in the original string become &gt;, &amp;, and &lt;. To also escape quotation marks, call html.escape(text, quote=True), which converts " and ' to &quot; and &apos;.

To unescape entities (converting them back to characters), use html.unescape():

s = 'Tom &amp; Jerry &copy; 2023'
print(html.unescape(s))  # 输出: Tom & Jerry © 2023

The html.unescape() function recognizes both named and numeric character references in the string and replaces them with their corresponding Unicode characters. In the example above, &amp; becomes & and &copy; becomes the © symbol.

Prior to Python 3.4, you could unescape HTML using HTMLParser().unescape() or xml.sax.saxutils.unescape(). Since Python 3.4, the standard library provides html.unescape() directly, and older methods are deprecated. Use html.escape() and html.unescape() in modern Python development.

Beyond manual function calls, most web frameworks provide automatic escaping. Django and Flask template engines automatically escape template variables, so developers avoid special characters breaking page structure or creating security issues (such as XSS vulnerabilities) without explicit function calls. When constructing HTML output manually, use these functions to handle entities correctly.

Common Issues and Considerations

Character entities are fundamental tools for representing special characters across text formats. From basic HTML symbols like &lt; to language-specific and symbol references, they enable correct display in diverse environments. In modern development, adapt your approach to context: use direct Unicode characters in systems that support UTF-8, and apply appropriate escaping when outputting HTML for safety. Mastering character entities ensures text content renders correctly across platforms and avoids common encoding pitfalls.

© 2026 Yuxu Ge ·