In this tutorial, we are going to have a look at python's ascii()
library method that returns a readable version of any object. This is a very simple and easy to learn function which can be very useful at times.
The ascii()
method in python returns a string containing printable representation (readable version) of an object (String, Tuple, List etc.). And all the non-ascii characters in the string are replaced using escape characters.
For example, ë
is converted to \xeb
by the ascii()
method.
Syntax:
ascii(object)
# object can be String, Dictionary, List, Tuple etc.
An important point to note here is that this method takes only one parameter i.e it can be String
or a Tuple
or a List
or any other object.
Lets see some more examples for this function with different types of objects.
1. Working with Strings
For the example below, we have added a few special characters to our string,
str = "S T Û D Y T Ó Ñ I G H T"
print (ascii(str))
Output:
'S T \xdb D Y T \xd3 \xd1 I G H T'
2. Working with Lists
For the example below, we have added a few special characters to our list,
list1 = ['Á', 'Ê', 'Õ', 'Ü']
print (ascii(list1))
Output:
['\xc1', '\xca', '\xd5', '\xdc']
Conclusion:
Pheww! This is it. I hope that you enjoyed the post and learned about the ascii()
ptyhon library function. If you feel that this post is useful, please share it with your friends and colleagues.
Thanks for reading it till the end.