ascii() method returns a string containing a printable representation of an object. However,
ascii() function escapes the non-ASCII characters in the string using
Python ascii() Syntax
ascii( object )
As we already mentioned,
ascii() method returns the printable version of a string escaping the non-ASCII character using
ascii() and repr()
Earlier in Python2,
repr() method was used to return the printable representation of an object but it didn’t escape the non-ASCII characters.
ë is represented as
repr() method, but by
ascii() method, it is represented by Python escape sequence
ö is represented as
Here is an example code.
>>> print(repr('ë')) 'ë' >>> print(ascii('ë')) '\xeb'
Python ascii() Method Example
>>> str1 = 'Python Unicode' >>> print(ascii(str1)) 'Python Programming' >>> str2 = 'Pythön Unicödë' >>> print(ascii(str2)) 'Pyth\xf6nn Unic\xf6nd\xeb'
As you can see in above script,
ö is represented by escape sequence
\xeb when used with Python