Python ascii() Function

Returns a printable version of an object

Usage

The ascii() function returns a string containing a printable version of an object. It replaces any non-ascii characters with escape characters \x, \u or \U

For example, German letter ß will be replaced with \xdf.

Syntax

ascii(object)

Python ascii() function parameters
ParameterConditionDescription
objectRequiredAny object (such as string, list, tuple, dictionary etc)

Basic Example

# Return a printable version by escaping non-ascii characters
S = 'Das straße'
x = ascii(S)
print(x)
# Prints 'Das stra\xdfe'

ascii() on List and Dictionary

The ascii() function accepts any object including list, dictionary, tuple etc.

# ascii() on list
L = ['The street', 'Das straße']
x = ascii(L)
print(x)
# Prints ['The street', 'Das stra\xdfe']
# ascii() on dictionary
D = {'EN':'The street', 'DE':'Das straße'}
x = ascii(D)
print(x)
# Prints {'EN': 'The street', 'DE': 'Das stra\xdfe'}