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 Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python frozenset()
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python zip()
Intelligent Projects Using Python - Santanu Pattanayak
Python Program to Print Hello world!
Python Inheritance
Python RegEx
Python Program to Measure the Elapsed Time in Python
Python Program to Find the Factorial of a Number
Python ord()
Python Program to Find Factorial of Number Using Recursion
Python Functions
Python Program to Calculate the Area of a Triangle
Python String partition()
Python Set isdisjoint()
Python String splitlines()
Python String isspace()
Python next()
Python Program to Swap Two Variables
Python String rsplit()
Python Program to Illustrate Different Set Operations
Python Deep Learning Cookbook - Indra den Bakker
Python Program to Catch Multiple Exceptions in One Line
Python Dictionary
Python *args and **kwargs
Python Program to Find the Size (Resolution) of a Image
Python Generators
Python datetime
Python Package
Python String isprintable()
Python Program to Create Pyramid Patterns