Python ascii() function

The ascii() function in Python returns a printable representation of the given object. It escapes characters with \x, \u or \U.

ascii() on strings

Example 1: string1 = 'Python is awesome' print(ascii(string1)) In this example the string is returned without changes, because no non-ASCII character is present. 'Python is awesome' Example 2: string2 = 'Python ist großartig' print(ascii(string2)) In this example, non-ASCII characters are escaped. 'Python ist gro\xdfartig' Example 3: string3 = 'Python ist gro\xdfartig' print(string3) In this example, the escaped string is printed. Python ist großartig

ascii() on lists

Example 1: list1 = ['Python', 'awesome', 'großartig'] print(ascii(list1)) In this example, the elements of the list are escaped (if necessary). ['Python', 'awesome', 'gro\xdfartig']

ascii() syntax

The syntax of the ascii() function is: ascii(object)

ascii() arguments

The ascii() functions accepts exactly one argument. If more or less than one argument is given, a TypeError occurs.

ascii() return value

The ascii() function returns the same object, and escapes non-ASCII characters using \x, \u or \U.



Click to jump to section