Table of Contents
The ascii() method returns a string containing a printable representation of an object. It escapes the non-ASCII characters in the string using \x, \u or \U escapes.
The syntax of ascii() is:
ascii(object)
1. ascii() Parameters
ascii() method takes an object (like: strings, list etc).
2. Return Value from ascii()
It returns a string containing a printable representation of an object.
For example, ö is changed to \xf6n, √ is changed to \u221a
The non-ASCII characters in the string are escaped using \x, \u or \U.
3. Example 1: How ascii() method works?
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
4. More Examples
randomList = ['Python', 'Pythön', 5] print(ascii(randomList))
Output
['Python', 'Pyth\xf6n', 5]
Related posts:
Python len()
Python Set update()
Python Shallow Copy and Deep Copy
Python Get Current time
Python String partition()
Python List extend()
Python Program to Make a Flattened List from Nested List
Python sorted()
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python Program to Convert Kilometers to Miles
Python staticmethod()
Python divmod()
Python issubclass()
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python Program to Measure the Elapsed Time in Python
Python Set issuperset()
Python String islower()
Python Program to Add Two Numbers
Python Program to Multiply Two Matrices
Python String rsplit()
Python Program to Capitalize the First Character of a String
Python Program Read a File Line by Line Into a List
Python Recursion
Python eval()
Python String istitle()
Python vars()
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python Program to Transpose a Matrix
Python Program to Add Two Matrices
Python Program to Check if a Number is Positive, Negative or 0
Python map()
Python String index()