DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk2 min

Show Unicode as Escapes in Python Output With ascii()

Python’s ascii() returns a repr()-style string with non-ASCII characters escaped. See its output, how it compares with str() and repr(), and how to use !a in f-strings.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

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.

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().

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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 !a when 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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.