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

Python basic tutorial

Python flow control

Função do Python

Tipos de Dados do 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, ö changed to \xf6n, √ 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 this 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 this program, the output is:

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

Python built-in functions