In Python, ascii(obj) returns the same text as repr(), except that every non-ASCII character is replaced by an escape sequence: \x for code points up to 255, \u up to 65535, and \U beyond that.
The result contains only plain ASCII, so it is safe to write to any log file or terminal, and it shows exactly which characters a string contains. For a string, the result includes the quotes, just like repr().
ascii() is a built-in function: it lives in the builtins module, so it is always available without an import.
Syntax
Pass any object; you get an ASCII-only string.
python
text = ascii(obj)
Call
What it does
ascii("cafe")
Prints 'cafe' (quotes included).
ascii("caf\u00e9")
Prints 'caf\xe9'.
ascii("\u20ac")
Prints '\u20ac'.
ascii([1, "\u00fc"])
Escapes inside containers too: [1, '\xfc'].
The escapes are valid Python string syntax, so the result can be pasted back into source code.
Library declarations
python
ascii(object, /)
# like repr(object), then non-ASCII characters escaped
Parameters
Parameter
Description
object
Required. Any Python object.
Return Value
Return Value
Description
str
The object's repr() with non-ASCII characters escaped using \x, \u, or \U.
Python Examples
Example 1 — Escape Accented and Non-Latin Text
Print four words with ascii() and see which escape each character gets.
python
words = ["cafe", "caf\u00e9", "na\u00efve", "\u65e5\u672c"]
for w in words:
print(ascii(w))
Output
'cafe'
'caf\xe9'
'na\xefve'
'\u65e5\u672c'
How It Works
Plain ASCII unchanged."cafe" needs no escapes, so the result is just its repr with quotes.
\x for Latin-1. é (U+00E9) and ï (U+00EF) are below 256, so they become \xe9 and \xef.
\u for larger code points. The two Japanese characters are U+65E5 and U+672C and become four-digit \u escapes.
Safe output. Every printed line is pure ASCII, so it displays the same on any console.
Example 2 — Escape Text Inside Containers
Pass a dict and a list to ascii() and compare string lengths before and after escaping.
Works on any object.ascii(data) builds the dict's repr and escapes the é and ü inside it.
Euro sign. € is U+20AC, written as \u20ac inside the list's repr.
Length grows."José" has 4 characters; its ASCII form 'Jos\xe9' has 9 (two quotes plus a four-character escape).
Readable debugging. You can see at a glance that the data contains non-ASCII characters.
Example 3 — Emoji, Custom Classes, and Control Characters
Escape a character outside the Basic Multilingual Plane, a custom __repr__(), and a tab.
python
class Product:
def __init__(self, name):
self.name = name
def __repr__(self):
return f"Product({self.name!r})"
print(ascii("\U0001F600"))
print(ascii(Product("cr\u00e8me")))
print(ascii("tab\there"))
print(ascii("plain") == repr("plain"))
Output
'\U0001f600'
Product('cr\xe8me')
'tab\there'
True
How It Works
\U for emoji. The grinning face is U+1F600, above 65535, so it needs the eight-digit \U0001f600 escape.
Custom repr escaped.ascii() calls Product.__repr__ and then escapes the è in its result.
Control characters. A tab is shown as \t, exactly as repr() would show it.
Same as repr for ASCII. For text that is already ASCII, ascii() and repr() return identical strings.
Frequently Asked Questions
Both return a developer-friendly representation. repr() keeps printable non-ASCII characters such as é as they are, while ascii() replaces them with \x, \u, or \U escapes.
No. It escapes them, so é becomes \xe9. To strip accents, use unicodedata.normalize() and filter combining characters.
It returns the repr of the object, and the repr of a string includes quotes. Use encode("ascii", "backslashreplace").decode() if you want escapes without quotes.
For strings, yes: ast.literal_eval(ascii(s)) returns the original text, because the escapes are valid Python string syntax.
For logging and debugging text that may contain invisible or unusual characters, and for output that must stay pure ASCII.