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 Program to Iterate Over Dictionaries Using for Loop
Python Program to Generate a Random Number
Python Program to Print all Prime Numbers in an Interval
Python range()
Python Program to Get File Creation and Modification Date
Python Program to Differentiate Between del, remove, and pop on a List
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python divmod()
Python Data Types
Python Tuple
Python Operators
Python Program to Return Multiple Values From a Function
Python Program to Find Numbers Divisible by Another Number
Python Program to Get a Substring of a String
Python Dictionary values()
Python time Module
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python locals()
Python Set intersection()
Python Functions
Python List reverse()
Python Inheritance
Python Variables, Constants and Literals
Python Program to Append to a File
Python map()
Python String casefold()
Python Package
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python Set intersection_update()
Python Object Oriented Programming
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python String zfill()