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

Example: Return a printable version by escaping non-ascii characters

S = 'Das straße'
x = ascii(S)
print(x)	# 'Das stra\xdfe'

ascii() on List and Dictionary

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

Example: ascii() on list

L = ['The street', 'Das straße']
x = ascii(L)
print(x)	# ['The street', 'Das stra\xdfe']

Example: ascii() on dictionary

D = {'EN':'The street', 'DE':'Das straße'}
x = ascii(D)
print(x)  # {'EN': 'The street', 'DE': 'Das stra\xdfe'}