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:
How to get current date and time in Python?
Python Program to Convert Celsius To Fahrenheit
Python String format()
Python String rindex()
Python List Comprehension
Python String encode()
Python Data Types
Python del Statement
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python if...else Statement
Python String isdecimal()
Python String isprintable()
Python getattr()
Python String find()
Python Program to Get the File Name From the File Path
Python Object Oriented Programming
Python Program to Parse a String to a Float or Int
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python String ljust()
Python String lstrip()
Python strptime()
Python String isupper()
Python Set isdisjoint()
Python Tuple index()
Python List extend()
Python Tuple
Python String endswith()
Python any()
Python Program to Get the Class Name of an Instance
Python Dictionary keys()
Python super()
Python id()