Python ascii() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Built-in

Definition

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)
CallWhat 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

ParameterDescription
objectRequired. Any Python object.

Return Value

Return ValueDescription
strThe 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))

How It Works

  1. Plain ASCII unchanged. "cafe" needs no escapes, so the result is just its repr with quotes.
  2. \x for Latin-1. é (U+00E9) and ï (U+00EF) are below 256, so they become \xe9 and \xef.
  3. \u for larger code points. The two Japanese characters are U+65E5 and U+672C and become four-digit \u escapes.
  4. 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.

python
data = {"name": "Jos\u00e9", "city": "M\u00fcnchen"}

print(ascii(data))
print(ascii(["\u20ac5", 10]))
print(len("Jos\u00e9"), len(ascii("Jos\u00e9")))

How It Works

  1. Works on any object. ascii(data) builds the dict's repr and escapes the é and ü inside it.
  2. Euro sign. € is U+20AC, written as \u20ac inside the list's repr.
  3. Length grows. "José" has 4 characters; its ASCII form 'Jos\xe9' has 9 (two quotes plus a four-character escape).
  4. 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"))

How It Works

  1. \U for emoji. The grinning face is U+1F600, above 65535, so it needs the eight-digit \U0001f600 escape.
  2. Custom repr escaped. ascii() calls Product.__repr__ and then escapes the è in its result.
  3. Control characters. A tab is shown as \t, exactly as repr() would show it.
  4. 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.

Next: bin()

Binary string of an integer, prefixed with 0b.

bin() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful