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 Closures
Python String ljust()
Python Program to Find Hash of File
Python bytes()
Python List pop()
Python Dictionary setdefault()
Python String isspace()
Python Program to Check Armstrong Number
Intelligent Projects Using Python - Santanu Pattanayak
Python any()
Python object()
Python Program to Calculate the Area of a Triangle
Python Program to Convert Celsius To Fahrenheit
Python Program to Capitalize the First Character of a String
Python String split()
Python String isidentifier()
Python Program to Find All File with .txt Extension Present Inside a Directory
Python super()
Python Program to Check if a Number is Odd or Even
Python eval()
Python String lower()
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python Program to Convert String to Datetime
Python list()
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Set intersection_update()
Python dict()
Python Set difference()
Python List index()
Python String isprintable()
Python memoryview()
Python Directory and Files Management