ascii() function replaces any non-ascii characters with the equivalent ascii values (using \x, \u or \U escape characters).
Example:
s = 'µ = B/H'
print(ascii(s)) # Output: '\xb5 = B/H'
Syntax
ascii(object)
Parameters
ascii() takes a single parameter; an object that can be a list, tuple, set, string, etc.
Return Value
- A string containing a printable representation of an object, but escape the non-ASCII characters in the string using \x, \u or \U escapes.
Example
Use of ascii() with different objects
data = '® represents Registered'
print(ascii(data))
# ® is replaced with ascii value
mylist = ['Häppÿ','Good','ÿÀ']
print(ascii(mylist))
constants_set = {'æ','ß','µ','Ø'}
print(ascii(constants_set))
Output
'\xae represents Registered'
['H\xe4pp\xff', 'Good', '\xff\xc0']
{'\xe6', '\xdf', '\xb5', '\xd8'}