Python ascii() Function: Syntax and 5 Examples

If you want to see exactly which characters hide inside a string in Python, the Python ascii function shows each one as plain text. It takes any object and gives you back its printable form, with every non-ASCII character swapped for an escape code.

Paste this into a Python file and run it:

print(ascii("Zoë"))
# 'Zo\xeb'

In this tutorial, I’ll show you accents, emoji escapes, hidden spaces, custom classes and common mistakes, run on Python 3.12.5.

Python ascii() Syntax

No import needed. Type it in your Python file:

ascii(object, /)
ParameterRequiredDefaultDescription
objectYes–Any object: string, number, list, dict or class

Return value: a str. It’s the same text repr() gives you, but non-ASCII characters become \x, \u or \U escapes.

Note: unlike the Python str() function, ascii() keeps quotes around strings.

When to Use ascii() in Python

  • Debugging accented names from a CSV file or API
  • Spotting non-breaking or zero-width spaces that break comparisons
  • Showing a list or dict exactly while testing

ascii() doesn’t give you a number. If you want the ASCII value of a character (65 for A), use the Python ord() function instead.

Example 1: Escape Accented Characters with ascii()

To run your first example in VS Code:

  1. In VS Code, choose File > New File.
  2. Paste the code and save it as ascii_basic.py.
  3. Click Run Python File at the top right.
customer = "José Martinez"

print(customer)
print(ascii(customer))

Output:

José Martinez
'Jos\xe9 Martinez'
Python ascii() function turning é in José Martinez into the \xe9 escape
print() shows the accent, ascii() shows the escape.

How does this work? print() showed é. ascii() swapped it for \xe9 (its hex code point) and added quotes.

To print ASCII-only text in Python, wrap the value first: print(ascii(x)).

Example 2: Use ascii() on a List of Customer Names

Save this as ascii_names.py and run it in VS Code:

names = ["Zoë Carter", "Renée Brooks", "Mark Diaz"]

print(names)
print(ascii(names))

Output:

['Zoë Carter', 'Renée Brooks', 'Mark Diaz']
['Zo\xeb Carter', 'Ren\xe9e Brooks', 'Mark Diaz']
Python ascii() on a list of US customer names with accents in each element
ascii() escapes every element, and plain names stay the same.

How does this work? print() showed the accents. ascii() escaped ë and é in every element, and Mark Diaz came back as is.

To drop those characters instead, see how to remove non-ASCII characters in Python.

Example 3: \x, \u and \U Escapes in Python ascii()

Save this as ascii_escapes.py and run it in VS Code:

notes = ["café", "€25.00 off", "Thanks 👍"]

for note in notes:
    print(ascii(note))

Output:

'caf\xe9'
'\u20ac25.00 off'
'Thanks \U0001f44d'

How does this work? é fits in two hex digits, so it got \xe9. The euro sign needs four (\u20ac), and the emoji needs eight (\U0001f44d).

For real bytes, use the Python string encode() method.

Example 4: Find Hidden Characters in Python Strings

These values look the same, but only one matches. Save this as ascii_hidden.py and run it in VS Code:

cities = ["Austin", "Austin ", "Aus​tin", "Austin\t"]

for city in cities:
    print(city == "Austin", f"[{city}]", ascii(city))

Output:

True [Austin] 'Austin'
False [Austin ] 'Austin\xa0'
False [Aus​tin] 'Aus\u200btin'
False [Austin	] 'Austin\t'
Python ascii() exposing a non-breaking space, a zero-width space and a tab in Austin values
print() hides the extra spaces, ascii() names them.

How does this work? Inside the brackets, the extra characters look like plain spaces or nothing. ascii() showed \xa0, \u200b and \t, so you know what to strip.

To test a value before cleaning it, check if a string is ASCII in Python.

Example 5: ascii() vs repr() with a Custom Class

ascii() uses your class’s __repr__. Save this as ascii_custom.py and run it in VS Code:

class Store:
    def __repr__(self):
        return "Store('Café Lumière', 'San José')"

print(repr(Store()))
print(ascii(Store()))

Output:

Store('Café Lumière', 'San José')
Store('Caf\xe9 Lumi\xe8re', 'San Jos\xe9')

How does this work? repr() kept é and è from __repr__. ascii() escaped them, and that’s the only difference.

Common Mistakes with Python ascii()

Save this as ascii_mistakes.py and run it in VS Code:

print(ascii("é") == "e", len(ascii("é")))
print(ascii(65), ord("A"))
try:
    ascii("a", "b")
except TypeError as err:
    print("TypeError:", err)

Output:

False 6
65 65
TypeError: ascii() takes exactly one argument (2 given)
Common Python ascii() mistakes: escape text, numbers and wrong argument count
Each line shows one mistake.
  1. ascii() doesn’t remove accents: "é" became 6 characters of escape text, not e.
  2. ascii(65) returns '65', not A. Use ord() or chr() for codes.
  3. ascii() takes exactly one argument.

To turn text into real ASCII codes, see how to convert a string to ASCII in Python.

Wrapping Up the Python ascii() Function

The Python ascii() function gives you the repr() of any object with non-ASCII characters escaped. It’s my first check when two strings look the same but don’t match.

Next, browse more Python built-in functions and methods.

Python ascii() Function Questions, Answered

What type does ascii() return in Python?

Always a str, even for a number or a list.

How do I return the ASCII value of a character in Python?

Use ord(). ord("A") returned 65, while ascii("A") returned 'A'.

Does ascii() work on bytes?

Yes. ascii(b"caf\xc3\xa9") returned b'caf\xc3\xa9', the same as repr().

How do I get plain ASCII text without escapes?

Encode with errors ignored: "José".encode("ascii", "ignore").decode() returned Jos.

Is ascii() the same as repr() in Python 3?

It matches repr() when the text is pure ASCII. See ascii() in the Python docs.

More character posts worth a look: