In this tutorial we will learn about the python ascii() method and its uses. 

Python ascii() Method

What is Python ascii() Method?

The ascii() method will return a readable version of a string containing a printable representation of an object.

The ascii() method will replace non-ASCII like å with escape characters like \x and \u.

The syntax of ascii() is as follows.


Python ascii() Parameter

The ascii() method will have a single parameter as an object like String, List, Tuple, Dictionary, etc.

Let’s see some examples of ascii() method.

Example 1: How to use ascii() method in the list.

my_list = ['Pythön','¥',2,'ASCII']



['Pyth\xf6n', '\xa5', 2, 'ASCII']

Example 2: using ascii() method with strings.

my_string = "µ"

my_string = "Pythön is Awësome"

The output will be as follow:

'Pyth\xf6n is Aw\xebsome'

As you can see, all the non-ascii values are replaced by escape characters.

Rules of ascii() method

  • It will return a readable string representation of an object.
  • It will not return anything when we use an integer as an object.


Was this article helpful?
Share on: