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:
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Python Keywords and Identifiers
Python Program to Check Leap Year
Python Program to Get Line Count of a File
Python Program to Check the File Size
Python __import__()
Python Program to Convert Two Lists Into a Dictionary
Python sum()
Python divmod()
Python String isupper()
Python RegEx
Python Package
Python Numbers, Type Conversion and Mathematics
Python Program to Get the File Name From the File Path
Python Program to Transpose a Matrix
Python Program to Return Multiple Values From a Function
Python Program to Delete an Element From a Dictionary
Python String rfind()
Python getattr()
Python Program to Solve Quadratic Equation
Python Data Types
Python Modules
Python Program to Illustrate Different Set Operations
Python Set update()
Introduction to Scientific Programming with Python - Joakim Sundnes
Python Program to Measure the Elapsed Time in Python
Python Program to Find Armstrong Number in an Interval
Python Set intersection_update()
Python Decorators
Python String capitalize()
Python Program to Check if a Key is Already Present in a Dictionary
Python vars()