Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

ascii()

Returns a printable representation with non-ASCII characters replaced by escapes.

Version: Python 3.14; repository image, production runtime not attested

Syntax

ascii(object, /)

Returns a printable representation with non-ASCII characters replaced by escapes.

A non-ASCII letter in a string becomes an escape in the representation; the original string remains Unicode.

ascii starts from repr and escapes all non-ASCII code points in that representation. The printed representation of café contains quotes and a literal backslash escape for é. No encoding or mutation of the input occurs, so the original still has four code points.

text = "café"
assert ascii(text) == "'caf" + chr(92) + "xe9'"
assert len(text) == 4 and text.endswith("é")

Examples

text = "café"
print(ascii(text))
print(len(text))

Result

'caf\xe9'
4

ascii starts from repr and escapes all non-ASCII code points in that representation. The printed representation of café contains quotes and a literal backslash escape for é. No encoding or mutation of the input occurs, so the original still has four code points.

Sources