ascii()
ascii(object) str · Updated March 13, 2026 · Built-in Functions The ascii() function returns a string containing a printable representation of an object. It escapes non-ASCII characters using \x, \u, or \U escape sequences, making it useful for debugging and logging.
Syntax
ascii(object)
Parameters
| Parameter | Type | Description |
|---|---|---|
object | any | Any Python object (string, list, dict, custom class, etc.) |
Examples
Basic usage with strings
text = "hello world"
print(ascii(text))
# 'hello world'
# Non-ASCII characters get escaped
text = "café"
print(ascii(text))
# 'caf\\xe9'
Working with unicode strings
# Emoji gets escaped
message = "Hello 👋"
print(ascii(message))
# 'Hello \\U0001f44b'
# Chinese characters
text = "你好"
print(ascii(text))
# '\\u4f60\\u597d'
With lists and dictionaries
data = ["apple", "café", 42]
print(ascii(data))
# ['apple', 'caf\\xe9', 42]
mixed = {"name": "José", "emoji": "🎉"}
print(ascii(mixed))
# {'name': 'Jos\\xe9', 'emoji': '\\U0001f389'}
With custom objects
class Person:
def __init__(self, name):
self.name = name
person = Person("Renée")
print(ascii(person))
# <__main__.Person object at 0x...>
Common Patterns
Debug logging with ascii()
import logging
logging.basicConfig(level=logging.DEBUG)
def process_user_input(user_data):
# Safely log user input that might contain special characters
logging.debug(f"Processing: {ascii(user_data)}")
# Output: Processing: {'name': 'J\\xf6rg', 'city': 'M\\xfcnchen'}
process_user_input({"name": "Jörg", "city": "München"})
Sanitizing output for logs
def log_response(response_data):
# ascii() ensures log files stay ASCII-safe
with open("app.log", "a") as f:
f.write(ascii(response_data) + "\n")
log_response("User said: héllo wörld")
# File contains: User said: h\\xe9llo w\\xf6rld
Comparing representations
# Compare objects ignoring Unicode normalization
a = "café"
b = "café" # Different unicode codepoint representation
print(a == b) # Might be False depending on input
print(ascii(a) == ascii(b)) # Useful comparison
n## How It Works
The function is similar to but with an important difference: it forces all non-ASCII characters to be escaped. This makes it useful when you need to guarantee that output contains only ASCII characters, such as writing to log files or debugging output that must be ASCII-safe.
When encounters a Unicode character, it uses the following escape sequences:
- for characters in the range 0x00-0xFF
- for characters in the range 0x0100-0xFFFF
- for characters beyond 0xFFFF
This escaping ensures output can be decoded by any ASCII-compatible encoding.
See Also
- built-in::repr — return a printable representation (preserves Unicode)
- built-in::chr — convert an integer to a character
- built-in::ord — convert a character to its Unicode codepoint
How It Works
The function is similar to but with an important difference: it forces all non-ASCII characters to be escaped. This makes it particularly useful when you need to guarantee that output contains only ASCII characters, such as when writing to log files or debugging output that must be ASCII-safe.
When encounters a Unicode character, it uses the following escape sequences:
- for characters in the range 0x00-0xFF (Latin-1)
- for characters in the range 0x0100-0xFFFF (BMP Unicode)
- for characters beyond 0xFFFF (supplementary planes like emoji)
This escaping ensures that the output can be decoded by any ASCII-compatible encoding, which is essential for compatibility with older systems or when debugging Unicode-related issues.