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 Safely Create a Nested Directory
Python Data Structures and Algorithms - Benjamin Baka
Python frozenset()
Python String swapcase()
Python Numbers, Type Conversion and Mathematics
Python Set difference_update()
Python String rindex()
Python Program to Convert Celsius To Fahrenheit
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python String find()
Python Program to Find the Factors of a Number
Python String partition()
Python String startswith()
Python Program to Parse a String to a Float or Int
How to Get Started With Python?
Python exec()
Python Program to Catch Multiple Exceptions in One Line
Python Program to Check Whether a String is Palindrome or Not
Python sleep()
Python Program to Generate a Random Number
Python Set discard()
Python String expandtabs()
Python String title()
Python callable()
Python Program to Access Index of a List Using for Loop
Python Program to Check If a List is Empty
Python Program to Find Hash of File
Deep Learning with Python - Francois Cholletf
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python Program to Differentiate Between type() and isinstance()
Python Program to Check if a Number is Positive, Negative or 0
Python String rpartition()