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 Strings
Python Program to Get the Last Element of the List
Python Program to Check Leap Year
Python Program to Print all Prime Numbers in an Interval
Python strftime()
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Program to Find the Sum of Natural Numbers
Python bool()
Python String partition()
Python globals()
Python memoryview()
Python list()
Python sorted()
Python Set symmetric_difference_update()
Python Set intersection()
Python Program to Generate a Random Number
Python *args and **kwargs
Python Errors and Built-in Exceptions
Python enumerate()
Python String capitalize()
How to Get Started With Python?
Python property()
Python sleep()
Python frozenset()
Python pass statement
Python String index()
Python Program to Sort Words in Alphabetic Order
Python hash()
Python Program to Return Multiple Values From a Function
Python Program to Transpose a Matrix
Python List index()
Python String encode()