Python ascii() function

The ascii() function in Python is used to replace any non-ASCII characters with escape characters. For example:

x = "cödëscråckêr.cöm"
print(ascii(x))

The characters ö, ë, å, and ê are non-ascii characters. Therefore, the function ascii() converts or replaces all these non-ASCII characters with escape characters. That is:

Here is the output produced by the above Python program, demonstrating the ascii() function:

'c\xf6d\xebscr\xe5ck\xear.c\xf6m'

Python ascii() function syntax

The syntax of the ascii() function in Python is:

ascii(obj)

The "obj" is any object, such as a list, string, tuple, dictionary, etc.

Python ascii() function example

Here's a simple Python ascii() function example:

x = ["cödes", "crackër", "dot", "com"]
x = ascii(x)
print(x)

Here is its sample output:

['c\xf6des', 'crack\xebr', 'dot', 'com']

Advantages of the ascii() function in Python

Disadvantages of the ascii() function in Python

Python Online Test


« Previous Function Next Function »