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 Tuple count()
Python Set copy()
Python bool()
Python String find()
Python Program to Find the Sum of Natural Numbers
Python Program to Measure the Elapsed Time in Python
Python object()
Python String isnumeric()
Python id()
Python Machine Learning - Sebastian Raschka
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Inheritance
Python Program to Check If Two Strings are Anagram
Python Set difference()
Python Generators
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python String splitlines()
Python Program to Convert String to Datetime
Python String split()
Python @property decorator
Python Namespace and Scope
Python Program to Get Line Count of a File
Python Program to Check Leap Year
Python Program to Find Armstrong Number in an Interval
Python List extend()
Python Operator Overloading
Python String title()
Python del Statement
Python Program to Print Output Without a Newline
Python next()
Python Program to Convert Decimal to Binary, Octal and Hexadecimal