The Python ascii function generates a string that provides a printable representation of a given object, while converting any non-ASCII characters in the string into their corresponding escape sequences using \x, \u, or \U formats.
Python ascii Function Syntax
For illustration purposes, let us consider an example that showcases the use of the ascii function in Python.
ascii(object)
Parameters
object: It requires an object such as strings, lists, and so forth.
Return
It provides a human-readable format of an object while substituting non-ASCII characters with the escape character.
Python ascii Function Example
In this section, we will explore various examples demonstrating how the Python ascii function operates within the Python programming language.
normalText = 'Python is interesting'
print(ascii(normalText))
otherText = 'Pyth�n is interesting'
print(ascii(otherText))
print('Pyth\xf6n is interesting')
Output:
'Python is interesting'
'Pyth\xf6n is interesting'
Pyth�n is interesting
Clarification: In the preceding illustration, variables like normalText and otherText hold string values, and the function retrieves the ASCII value of a specific variable.