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 Dictionary pop()
Python Recursion
Python Exception Handling Using try, except and finally statement
Python sleep()
Python Program to Find ASCII Value of Character
Python Program to Get the Class Name of an Instance
Python RegEx
Python Program to Print Output Without a Newline
Python Function Arguments
Python Program to Make a Flattened List from Nested List
Python Program to Differentiate Between del, remove, and pop on a List
Python Global, Local and Nonlocal variables
Python String rstrip()
Python slice()
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python String upper()
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Anonymous / Lambda Function
Python Custom Exceptions
Python timestamp to datetime and vice-versa
Python String partition()
Python String splitlines()
Python String find()
Python Program to Sort a Dictionary by Value
Python str()
Python List Comprehension
Python Namespace and Scope
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python any()
Python Program to Check if a Number is Positive, Negative or 0
Python Set symmetric_difference()
Python Dictionary update()