Python ascii Function

Complete guide to Python's ascii function covering string conversion, non-ASCII handling, and practical examples of ASCII representation.

Python ascii Function

Python ascii Function

Last modified April 11, 2025

This comprehensive guide explores Python’s ascii function, which returns a string containing a printable representation of an object. We’ll cover string conversion, non-ASCII handling, and practical examples of ASCII escape sequences.

Basic Definitions

The ascii function returns a string containing a printable representation of an object, with non-ASCII characters escaped. It works similarly to repr but escapes non-ASCII characters.

Key characteristics: converts any object to its ASCII representation, escapes non-ASCII characters using \x, \u or \U escapes, and always returns a string. It’s useful for debugging and serialization.

Basic String Conversion

Here’s simple usage showing how ascii handles ASCII and non-ASCII strings differently from repr and str.

basic_ascii.py

ASCII string

print(ascii(“hello”)) # ‘hello’

Non-ASCII string

print(ascii(“héllö”)) # ‘h\xe9ll\xf6’

Comparison with repr

print(repr(“héllö”)) # ‘héllö’ print(str(“héllö”)) # héllö

This example shows ascii converting strings to ASCII-only representation. Non-ASCII characters are escaped using \x sequences.

The difference from repr is clear - repr preserves the original characters while ascii escapes them.

Non-ASCII Characters Handling

This example demonstrates how ascii handles various non-ASCII characters from different Unicode ranges.

non_ascii.py

Latin-1 Supplement (é)

print(ascii(“café”)) # ‘caf\xe9’

Greek (π)

print(ascii(“πr²”)) # ‘\u03c0r\xb2’

Emoji (😊)

print(ascii(“Hello 😊”)) # ‘Hello \U0001f60a’

Chinese (你好)

print(ascii(“你好”)) # ‘\u4f60\u597d’

The function escapes characters differently based on their Unicode code point. Latin-1 uses \x, BMP uses \u, and higher planes use \U.

This behavior ensures the output string contains only ASCII characters while preserving all information through escape sequences.

Custom Objects with repr

ascii works with custom objects by calling their repr method and then escaping the result. This example shows the behavior.

custom_ascii.py

class Person: def init(self, name): self.name = name

def __repr__(self):
    return f"Person({self.name})"

p1 = Person(“Alice”) p2 = Person(“Álice”)

print(ascii(p1)) # ‘Person(Alice)’ print(ascii(p2)) # ‘Person(\xc1lice)’

The Person class implements repr. When we call ascii on instances, it first gets the representation string then escapes non-ASCII.

This shows how ascii can be used with custom objects while maintaining ASCII-only output.

Container Types

When used with containers like lists or dictionaries, ascii escapes non-ASCII characters in all contained elements.

containers.py

data = [ “café”, {“key”: “välue”}, (1, “π”), {“α”, “β”} ]

print(ascii(data))

[‘caf\xe9’, {‘key’: ‘v\xe4lue’}, (1, ‘\u03c0’), {’\u03b1’, ‘\u03b2’}]

The example shows ascii recursively processing container elements. Each string element gets escaped if it contains non-ASCII characters.

This behavior is useful when you need to serialize data while ensuring ASCII output, such as for certain network protocols.

Error Handling

The ascii function doesn’t raise errors for any input type as it relies on the object’s repr method.

errors.py

class BadRepr: def repr(self): return “Bad: café”

print(ascii(BadRepr())) # ‘Bad: caf\xe9’

print(ascii(123)) # ‘123’ print(ascii([1, 2, 3])) # ‘[1, 2, 3]’ print(ascii(None)) # ‘None’

These examples demonstrate ascii’s behavior with different types. It first gets the string representation then escapes non-ASCII characters.

Even with a custom repr that returns non-ASCII, ascii will escape those characters in the final output.

Best Practices

  • Use for debugging: When you need ASCII-only output for non-ASCII data

  • Prefer repr: When you want to preserve original characters

  • Implement repr: For custom objects to control ASCII output

  • Consider encode: When you need bytes instead of escaped strings

  • Document usage: Clearly indicate when ASCII output is required

Source References

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Python tutorials.

ad ad