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() 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 |
If a non-ascii character is present in the object it will be converted to printable format.
obj = "I löve lèarning Pythön" print(obj)
print(ascii(obj))
Output:
I löve lèarning Pythön 'I l\xf6ve l\xe8arning Pyth\xf6n'
obj = "I love learning Python" print(obj)
print(ascii(obj))
Output:
I love learning Python 'I love learning Python'
tup_obj = ('L3arn', 'Lèarn', 3) print(ascii(tup_obj))
Output:
('L3arn', 'L\xe8arn', 3)