ascii() ASCII representation

Last updated: 2026-09-22

ascii() ASCII representation

ascii() is similar to repr(), but escapes non-ASCII characters as \x, \u, or \U sequences.

CategoryBuilt-in Functions
KindBuilt-in Function
Python Versionall

📝 Syntax

ascii(object)

⚙️ Parameters

object RequiredAny Python object.

Returns:str. The repr() of the object, with non-ASCII characters escaped.

💥 Raises

▶ Example

Edit the code, press Run, and see the result in the output panel:

Output:

'caf\xe9'
'\u4e2d\u6587'
More case studies coming soon — check back later.

❓ FAQ

QWhat is the difference between ascii() and repr()?
Arepr() returns a printable representation of the object and keeps non-ASCII characters; ascii() additionally escapes every non-ASCII character into \x, \u, or \U sequences, guaranteeing pure-ASCII output.
QWhat does ascii('中文') return?
AIt returns '\u4e2d\u6587' (with quotes) — the Chinese characters are escaped into \u sequences rather than kept as-is. To restore them use eval() or json.loads.
QWhat is ascii() useful for?
AIt is useful for log output, confirming a string contains only ASCII characters before cross-system transfer, and debugging invisible or non-printable characters.
QDoes ascii() affect a pure-ASCII string?
ANo. Output for pure-ASCII content matches repr() exactly; escaping only happens when non-ASCII characters are encountered.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏