Python ascii() Method – [With Example]

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.

ascii(object)

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’]
print(ascii(my_list))

Output:

[‘Pyth\xf6n’, ‘\xa5’, 2, ‘ASCII’]

Example 2: using ascii() method with strings.

my_string = “µ”
print(ascii(my_string))
my_string = “Pythön is Awësome”
print(ascii(my_string))

The output will be as follow:

‘\xb5’
‘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.