Python 3 reference · Built-in Functions

Python ascii() Function

Return a printable representation of an object, like repr(), but with every non-ASCII character escaped.

Python 3 Since 3.0

Syntax

ascii(object)

Parameters

ParameterDescription
object Any object.

Return value

A str containing only ASCII characters.

Examples

Compared with repr()Try it yourself
print(repr("café"))
print(ascii("café"))

Output

'café'
'caf\xe9'
Escaping emoji and accents in a listTry it yourself
print(ascii(["naïve", "\U0001F600", "ok"]))  # an emoji, written as an escape

Output

['na\xefve', '\U0001f600', 'ok']

When to use it

Use ascii() when output must be plain ASCII, for example in some log files or old terminals. Non-ASCII characters become escapes such as \xe9 or \U0001f600.

Version history

VersionChange
Python 3.0Added