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 Program to Find Armstrong Number in an Interval
Python Program to Sort Words in Alphabetic Order
Python Data Types
Python File I/O Operation
Python repr()
Python Tuple
Python Program to Convert String to Datetime
Python String count()
Python Program to Randomly Select an Element From the List
Python round()
Python map()
Python Machine Learning - Sebastian Raschka
Python format()
Python String splitlines()
Python Program to Find the Square Root
Python Program to Check if a Number is Positive, Negative or 0
Python String encode()
Python all()
Deep Learning with Python - Francois Cholletf
Python Tuple count()
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Python Program to Display Fibonacci Sequence Using Recursion
Python String rsplit()
Python pass statement
Python List remove()
Python staticmethod()
Python callable()
Python exec()
Python Program to Differentiate Between type() and isinstance()
Python Program to Find Factorial of Number Using Recursion
Python Inheritance