Python Reference Library →

Python ascii() Function – Get ASCII Representation of Objects

Let’s look at ascii() function – get ascii representation of objects

  1. Notice

    ascii() produces a printable representation, escaping non-ASCII characters. It returns a string rather than changing the source object.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = 'café'
print(ascii(text))
Live state

Top-level scope

text→Object 1
Object 1 · str'café'
This step

ascii() produces a printable representation, escaping non-ASCII characters. It returns a string rather than changing the source object.

Picture ascii() function – get ascii representation of objects

ascii() produces a printable representation, escaping non-ASCII characters. It returns a string rather than changing the source object.

Image brief

Alt text
A diagram for Python ascii() Function – Get ASCII Representation of Objects. Instructions: text = 'café' print(ascii(text)). Displayed result: 'caf\xe9'. The final example bindings are text --> object 1 (str). object 1: 'café'
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python ascii() Function – Get ASCII Representation of Objects". Teaching focus: ascii() produces a printable representation, escaping non-ASCII characters. It returns a string rather than changing the source object. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = 'café' print(ascii(text)) | v DISPLAYED RESULT 'caf\xe9' FINAL BINDINGS text --> object 1 (str) object 1: 'café' Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python ascii() Function – Get ASCII Representation of Objects. Instructions: text = 'café' print(ascii(text)). Displayed result: 'caf\xe9'. The final example bindings are text --> object 1 (str). object 1: 'café'

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = 'café'
print(ascii(text))
Live state

Top-level scope

text→Object 1
Object 1 · str'café'
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
'caf\xe9'

What do you expect this program to print?

What does this example display?

text = 'café'
print(ascii(text))
What does this example display?
Choose one answer. You can try again.

Answer and explanation

'caf\xe9'

Follow the printed values in order. The output is: 'caf\xe9'

More examples and reference

Python ascii() Function

The ascii() function returns a string that contains a printable representation of an object, but escapes non-ASCII characters using \x, \u, or \U escapes.

It is especially useful when you want to view or debug strings that contain non-English or special characters.

Syntax

ascii(object)

Parameter:

  • object – Any Python object (commonly a string or list)

Returns:

  • A string that contains the ASCII-only representation of the object
  • All non-ASCII characters are escaped

Example 1: ASCII of a string with special characters

text = "café"
print(ascii(text))
'caf\xe9'

Here, the accented character é is escaped as \xe9.

Example 2: ASCII of emojis or symbols

print(ascii("Python 🐍"))
'Python \U0001f40d'

Example 3: ASCII of a list with strings

names = ["José", "Zoë", "Müller"]
print(ascii(names))
['Jos\xe9', 'Zo\xeb', 'M\xfcller']

ascii() vs repr()

The repr() function returns a string representation, but keeps non-ASCII characters as-is if possible. In contrast, ascii() always escapes them.

print(repr("café"))   # 'café'
print(ascii("café"))  # 'caf\xe9'

Use Cases of ascii()

  • Debugging strings with special/non-English characters
  • Serializing or logging readable representations of objects
  • Ensuring output contains only ASCII-safe text

Common Mistakes

  • Confusing ascii() with ord() – they are different
  • ascii() does not encode or convert characters; it only escapes them for safe viewing

Interview Tip

Know the difference between str(), repr(), and ascii(). Interviewers often ask about string representation functions and how they behave with special characters.

Summary

  • ascii() returns a printable representation of an object
  • Escapes all non-ASCII characters using escape codes
  • Useful for debugging and safe printing/logging

Practice Problem

Try printing ASCII-safe versions of the following:

words = ["niño", "français", "中文"]
for word in words:
    print(ascii(word))

Expected Output:

'ni\xf1o'
'fran\xe7ais'
'\u4e2d\u6587'

What to remember

Connect what you noticed in the example with the idea below.

Remember

ascii() produces a printable representation, escaping non-ASCII characters. It returns a string rather than changing the source object.

Watch out for this idea

ascii() deletes every non-ASCII character.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →