ascii()
function is used to find the string containing a printable representation of an object in Python for the given input (object
– parameter). It uses \x, \u or \U escapes to escape the non-ASCII
characters in the string.
ascii()
function is a part of Python Built-in Functions (check list)
Syntax of ascii()
Function
The syntax of ascii()
function in Python is:
ascii(object)
Parameters of ascii()
Function in Python
object
– Where object
is any object in Python i.e. strings, lists etc. This parameter is required.
Compatibility
ascii()
function is only available and compatible with Python 3.x
.
Return Value of ascii()
Function in Python
ascii()
function returns a string containing a printable representation of an object for the given input (object
– parameter). The non-ASCII
character will be escaped in string by using \x, \u or \U.For example,
ñ
is changed to \xf1
and ß
is changed to \xdf
.
Python ascii()
Function Example 1
#Here ë is a unicode character print(ascii('This is my text with unicodë character')) #By ascii() function ë will be changed to \xeb escape print('This is my text with unicod\xeb character')
Output:
'This is my text with unicod\xeb character'
This is my text with unicodë character
Python ascii()
Function Example 2
# µ is a unicode character micro = 'µ is a micro symbol' print(ascii(micro)) # µ changed to \xb5 escape print('\xb5 is a micro symbol')
Output:
'\xb5 is a micro symbol'
µ is a micro symbol