Tutorial Study Image

Python ascii()

The ascii function takes an object as input and returns the object in printable format. The function finds any non-ascii characters in the object and replaces the character with corresponding escape characters.

ascii(object) #where object can be a string, list, tuple etc 

ascii() Parameters:

ascii() function has one and only one mandatory parameter. It takes as parameter an object such as string, tuple, list dictionary etc.

Parameter Description Required / Optional
object Any object, like String, List, Dictionary etc. Required

ascii() Return Value

If a non-ascii character is present in the object it will be converted to printable format.

Examples of ascii() method in Python

Example 1 : Passing a string with non-ascii characters

obj = "I löve lèarning Pythön" print(obj)


I löve lèarning Pythön
'I l\xf6ve l\xe8arning Pyth\xf6n'

Example 2 : Passing a string containing only ascii characters

obj = "I love learning Python" print(obj)


I love learning Python
'I love learning Python'

Example 3 : Passing an iterable with ascii and non-ascii strings and an integer

tup_obj = ('L3arn', 'Lèarn', 3) print(ascii(tup_obj))


('L3arn', 'L\xe8arn', 3)