Python ascii() Function with Examples

The ascii() function in python is used to return any object in readable or printable version. And if there is any non-ascii character then the....

In this tutorial, we will learn about the ascii in python programming language with the help of examples.

Python acsii() function

Python acsii() function

The function in python is used to return any object in readable or printable version. And if there is any non-ascii character then the python ascii function will replace that character with the escape character like \x, \u, \U etc.

Syntax

ascii(object)

Parameter of ascii() function in python

The built-in python function take objects as an argument such as list, strings, etc.

Return value of Python ascii() function 

• It return any object in readable or printable version.

• If there is any non-ascii character then it will replace that character with the escape character like \x, \u, \U etc.

• For example- √ is replaced with \u221a.

Python ascii() Function Examples

In the following, we have discussed about the most common ASCII value with the help of examples;

Example 1: Displaying ASCII value from the given string

str = "A ñ s w ê r s j ē t"

print (ascii(str))

Output

'A \xf1 s w \xea r s j \u0113 t'

Example 2: Taking variable with multiline string and pass it into the ascii() 

str = '''Python

is

programming 

language'''

print(ascii(str))

Output

'Python\nis\nprogramming \nlanguage'