Technology3 min read

Python ascii() Method - Python Library Function

Abhishek Ahlawat

By Abhishek Ahlawat — Recently

Founder of StudyTonight and a MERN fullstack developer. Writes about JavaScript, web development, Linux, and tech.

Summarize this article using AI

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.

ascii python method

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.


SHARE YOUR THOUGHTS WITH US!


Spotted something outdated or wrong?

Liked this? See more StudyTonight posts in Google results.

Add StudyTonight as a preferred source
Next questLIVE OPSStep 2 / 3

Practice real incidents in the browser

System Design Ops — put out fires, ship fixes, and stack XP. Reading done. Time to play.

Play System Design
  • +XP

    Every sim

  • Incidents

    Live ops

  • Ranks

    Climb up

Read next