Free tools Windows power users keep installed
One-click scans. No signup required.
Python’s ascii() returns a printable representation of an object like repr(), but replaces non-ASCII characters in that representation with escape sequences. It returns a Python string—not ASCII-encoded bytes—and is useful when you want Unicode characters to appear visibly as escapes in diagnostic output.
What does Python ascii() do?
The current Python 3.14.7 built-in functions reference gives the signature ascii(object, /). The slash means the argument is positional-only. The function returns a string containing a printable representation, following repr() and escaping non-ASCII characters in the resulting representation with x, u, or U notation. See the Python 3.14.7 built-in functions documentation.
As an Amazon Associate I earn from qualifying purchases.
For example, Python’s built-in types documentation uses this string:
text = "¡kočka 😸!"
print(ascii(text))
The resulting representation is:
'xa1kou010dka U0001f638!'
The outer quote characters are part of the representation. ASCII letters and punctuation remain readable; the inverted exclamation mark, č, and emoji appear as escapes. This is a representation of the original string, not a conversion of it into ASCII bytes. The example is documented in the Python 3.14 built-in types reference.
#1 Best Overall
How is ascii() different from str() and repr()?
Choose among the three based on whether you want display text or a representation, and whether non-ASCII characters should remain visible or appear escaped.
| Call | Purpose | Non-ASCII characters |
|---|---|---|
str(value) |
Ordinary text, commonly for user-facing display | Usually remain as Unicode characters |
repr(value) |
Printable representation, commonly useful for debugging | May remain as Unicode characters |
ascii(value) |
Printable representation following repr() |
Escaped in the representation |
For many built-in types, repr() aims to resemble a valid expression that could recreate the value, but that is not guaranteed for every object. ascii() keeps the representation behavior and changes how non-ASCII characters in the representation are shown.
Rank #2
How do you use ascii() in an f-string?
Use the !a conversion flag to request ascii() inside a formatted string. For the same value, !s requests str(), while !r requests repr().
text = "¡kočka 😸!"
print(f"{text = !a}")
print(f"{text = !r}")
print(f"{text = !s}")
The Python 3.14 built-in types reference documents the !a conversion as producing a debug-style label followed by the escaped representation. Its f-string documentation covers the !s, !r, and !a conversions in the same context: formatted string literals.
When should you use it—and when should you not?
- Use
str(value)when you want ordinary text for display. - Use
repr(value)when you want a printable representation, such as for debugging. - Use
ascii(value)or!awhen you want non-ASCII characters in that representation to be visible as escapes.
ascii() is not a Unicode normalizer, an encoder, or a security sanitizer. It does not make a value safe for HTML, SQL, shell commands, or another output context. Use the appropriate encoding or context-specific escaping for those tasks.
Which Python versions include ascii()?
The Python 3.13 built-in functions reference records that ascii() was removed in Python 3.0 and brought back in Python 3.2, so it is available from Python 3.2 onward. That version-history note is in the Python 3.13 built-in functions documentation. In the Python 3.14.7 reference, the positional-only marker appears in ascii(object, /); older documentation may show the signature without it.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
Recommended Free Tools




