Python 3.0+Built-in functionsterm
ascii
strings
Returns repr() of the object but replaces every non-ASCII character with a \xNN, \uNNNN or \UNNNNNNNN escape sequence.
Syntax
ascii(object)
What you get back is a repr, quotes included — fine for display, useless for comparing or counting characters. Reach for it when the output must be pure ASCII (a log file, a console that mangles Unicode) or when you need to spot invisible characters and look-alike letters; the !a conversion inside an f-string does exactly the same thing.
Examples
print(ascii('hello')) # → 'hello'
print(ascii('привет')) # → '\u043f\u0440...' (экранировано)
print(ascii([1,'a'])) # → [1, 'a']
print(ascii('café')) # → 'caf\xe9'
print(ascii(None)) # → None
See also
Open in the full glossarydocs.python.orgReport an inaccuracy