Python ascii() built-in function

From the Python 3 documentation

As repr(), return a string containing a printable representation of an object, but escape the non-ASCII characters in the string returned by repr() using \x, \u, or \U escapes.

Introduction

The ascii() function in Python is a built-in function that returns a string containing a printable representation of an object, similar to repr(). However, ascii() escapes any non-ASCII characters with \x, \u, or \U escape sequences. This is useful for ensuring that a string is safe to be used in an ASCII-only environment.

Examples

# For an ASCII character, it's the same as repr()
ascii('A')

# For a non-ASCII character, it gets escaped
ascii('ë')

# For comparison, repr() would not escape it
repr('ë')

# It works on iterables too
ascii(['A', 'ë'])
'A'
'\\xeb'
'ë'
['A', '\\xeb']