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 character ö, ë, å, ê are non-ascii characters. Therefore the function ascii() converts/replaces all these non-ascii characters with escape characters. That is:

  • ö replaced with \xf6
  • ë replaced with \xeb
  • å replaced with \xe5
  • ê replaced with \xea

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

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

Python ascii() Function Syntax

The syntax of ascii() function in Python is:

ascii(obj)

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

Python ascii() Function Example

Here is a simple example of ascii() function in Python:

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

Here is its sample output:

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

Python Online Test


« Previous Function Next Function »



Like/Share Us on Facebook 😋