Python ascii() Method
In this tutorial we will learn about the python ascii() method and its uses.
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.
1
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.
1
2
3
my_list = ['Pythön','¥',2,'ASCII']
print(ascii(my_list))
Output:
1
['Pyth\xf6n', '\xa5', 2, 'ASCII']
Example 2: using ascii() method with strings.
1
2
3
4
5
my_string = "µ"
print(ascii(my_string))
my_string = "Pythön is Awësome"
print(ascii(my_string))
The output will be as follow:
1
2
'\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.
This post is licensed under CC BY 4.0 by the author.