The ascii() Function in Python returns readable representation of an object (String, tuple, list, dictionary etc.). In this tutorial we will see the usage of ascii function with the help of examples.
Python ascii() function example
In the following example we have two strings
myText. The first string contains a non-ascii character and the second string is just a plain text with no non-ascii characters. As you can see in the output that the non-ascii character is replaced with a readable representation of the non-ascii character.
The non-ascii characters in a string are escaped using \x, \u or \U, as shown in the following example (the third print statement).
# A string containing non-ascii character myString = 'Ä Book' print(ascii(myString)) # A normal string myText = "This is a plain text" print(ascii(myText)) # Displaying a string using print that # contains ascii value print('\xc5 Book')
Python ascii function example with list and tuple
# tuple t = ('A', 'B', 'Ä') print(ascii(t)) # list lis = ['BeginnersBook', 'BeginnersBöök'] print(ascii(lis))
Leave a Reply