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
| Parameter | Description |
|---|---|
object |
Any object. |
Return value
A str containing only ASCII characters.
Examples
print(repr("café"))
print(ascii("café"))
Output
'café' 'caf\xe9'
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
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
repr()· Return the developer-friendly string representation of an object, often valid Python code that recreates the value.str()· Convert a value to its readable text form, or decode bytes into a string. Strings are immutable sequences of characters.ord()· Return the Unicode code point (an integer) of a single character, such as ord('A') giving 65. The inverse of chr().chr()· Return the one-character string for a Unicode code point, such as chr(65) giving 'A'. The inverse of ord().- Python Strings · tutorial