Python ascii() Function

ascii() is a Python built-in function to return a string value which is a printable representation of an object argument and escapse non-ASCII charaters in the return string.

Example 1 - ascii() with non-ASCII charater

x = ascii('ê')
print(x)

x = ascii('ɸ')
print(x)
Output:

'\xea'
'\u0278'

Example 2 - ascii() with ASCII charaters

x = ascii('abc')
print(x)

x = ascii('123')
print(x)
Output:

'abc'
'123'

Example 3 - ascii() with Unicode string

x = ascii('Việt Nam')
print(x)

x = ascii('日本')
print(x)

x = ascii('대한민국')
print(x)

x = ascii('中国')
print(x)
Output:

'Vi\u1ec7t Nam'
'\u65e5\u672c'
'\ub300\ud55c\ubbfc\uad6d'
'\u4e2d\u56fd'

Example 4 - ascii() with list object

data_list = ['Việt Nam', '日本', '대한민국', '中国', 'United State']
x = ascii(data_list)
print(x)
Output:

['Vi\u1ec7t Nam', '\u65e5\u672c', '\ub300\ud55c\ubbfc\uad6d', '\u4e2d\u56fd', 'United State']

Example 5 - ascii() with set object

data_set = {'Việt Nam', '日本', '대한민국', '中国', 'United State'}
x = ascii(data_set)
print(x)
Output:

{'\ub300\ud55c\ubbfc\uad6d', '\u65e5\u672c', 'United State', '\u4e2d\u56fd', 'Vi\u1ec7t Nam'}

Example 6 - ascii() with tuple object

data_tuple = ('Việt Nam', '日本', '대한민국', '中国', 'United State')
x = ascii(data_tuple)
print(x)
Output:

('Vi\u1ec7t Nam', '\u65e5\u672c', '\ub300\ud55c\ubbfc\uad6d', '\u4e2d\u56fd', 'United State')

Example 7 - ascii() with dict object

data_dict = {'country_1': 'Việt Nam', 'country_2': '日本', 'country_3': '대한민국', 'country_4': '中国', 'country_5': 'United State'}
x = ascii(data_dict)
print(x)
Output:

{'country_1': 'Vi\u1ec7t Nam', 'country_2': '\u65e5\u672c', 'country_3': '\ub300\ud55c\ubbfc\uad6d', 'country_4': '\u4e2d\u56fd', 'country_5': 'United State'}
Tags: