English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python basic tutorial

Python flow control

Fonctions en Python

Types de données en Python

Python file operations

Python objects and classes

Python date and time

Advanced Python knowledge

Python reference manual

Python ascii() usage and examples

Python built-in functions

The ascii() method returns a string containing the printable representation of the object. It uses \x, \u, or \U escape characters to escape non-ASCII characters in the string.

The syntax of ascii() is:

ascii(object)

ascii() parameters

The ascii() method takes an object (such as:string,listetc.).

ascii() return value

It returns a string containing the printable representation of the object.

For example, ö can be changed to \xf6n, √ can be changed to \u221a

Non-ASCII characters in strings are escaped using \x, \u, or \U.

Example1How does the ascii() method work?

normalText = 'Python is interesting'
print(ascii(normalText))
otherText = 'Pythön is interesting'
print(ascii(otherText))
print('Pyth\xf6n is interesting')

When running the program, the output is:

'Python is interesting'
'Pyth\xf6n is interesting'
Pythön is interesting

More examples

randomList = ['Python', 'Pythön', 5]
print(ascii(randomList))

When running the program, the output is:

['Python', 'Pyth\xf6n', 5]

Python built-in functions