Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Repair Windows errors before they cause bigger problemsFix Now →Scan for outdated or missing drivers - takes under a minuteDriver Scan →iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
Python’s ascii() returns a printable representation of an object, like repr(), but renders non-ASCII characters in that representation as escape sequences. It returns a Python string—not ASCII-encoded bytes—and is useful when you want a diagnostic representation to show those characters explicitly.
What does Python ascii() do?
The built-in function has the Python 3.14.7 signature ascii(object, /); the slash means its argument is positional-only. It returns a string containing a printable representation of the object. Its behavior follows repr(), then escapes non-ASCII characters in the representation using forms such as x, u, and U. See the Python 3.14.7 built-in functions reference.
For example, the Python documentation uses this string:
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →text = "¡kočka 😸!"
print(ascii(text))
The representation is:
'xa1kou010dka U0001f638!'
The outer quotes are part of the representation. ASCII letters and punctuation remain readable; the inverted exclamation mark, accented character, and emoji appear as escapes. This is a representation of the original string, not a change to its underlying text. The example and output are shown in the Python 3.14 built-in types documentation.
#1 Best Overall
How is ascii() different from str() and repr()?
Choose among the three based on whether you need ordinary display text or a representation intended to expose details about a value. The precise result depends on the object’s type and its representation methods.
| Conversion | What it requests | Typical use |
|---|---|---|
str(value) |
The object’s ordinary string conversion. | User-facing text. |
repr(value) |
A printable representation. | Diagnostics or inspection; for many built-in types, it aims to resemble an expression that could recreate the value, but that is not guaranteed for every object. |
ascii(value) |
A repr()-style representation with non-ASCII characters escaped. |
Diagnostics where non-ASCII code points should be visible as escapes. |
For a string containing Unicode characters, str() is generally suited to displaying the text itself, while repr() gives a representation and ascii() makes non-ASCII characters in that representation explicit. The Python documentation defines ascii() in relation to repr(), rather than as a separate text-encoding operation.
Rank #2
How do you use ascii() in an f-string?
In an f-string conversion, !a calls ascii(). The alternatives are !s for str() and !r for repr(). Python’s documented debug-style example is:
string = "¡kočka 😸!"
f'{string = !a}'
The result includes the label and the escaped representation:
"string = '\xa1ko\u010dka \U0001f638!'"
These f-string conversions request different forms of conversion; they do not change the string stored in string. The syntax and example appear in the Python 3.14 formatted string literals documentation.
Does ascii() encode text as ASCII?
No. It returns a string containing a representation. Escape sequences such as u010d are characters in that returned string; the function does not return ASCII bytes or encode the original text. If you need bytes in a particular encoding, use an encoding operation designed for that purpose rather than treating ascii() as an encoder.
When should you use it—and when should you avoid it?
- Use
str(value)or ordinary string conversion when the goal is readable, user-facing text. - Use
repr(value)for a printable representation when escaping non-ASCII characters is not specifically needed. - Use
ascii(value)or an f-string’s!aconversion when you want non-ASCII characters in a representation to appear as escapes. - Do not treat
ascii()as Unicode normalization or as a security sanitizer. Escaping for a Python representation does not make a value safe for HTML, SQL, shell commands, or another output context; use the escaping or parameterization rules for that context.
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. It is therefore available from Python 3.2 onward. The current Python 3.14.7 reference shows the positional-only signature ascii(object, /); older documentation may show the signature without the slash. See the Python 3.13 built-in functions reference.
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Repair Windows errors before they cause bigger problems3Fix the driver behind crashes, sound loss and screen glitchesQuick 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.

