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 Package
Python Program to Get the Class Name of an Instance
Python Program to Count the Number of Occurrence of a Character in String
Python Machine Learning Eqution Reference - Sebastian Raschka
Python Sets
Python repr()
Python Program to Check if a Number is Positive, Negative or 0
Python id()
Python @property decorator
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python Dictionary setdefault()
Python Program to Compute the Power of a Number
Python Function Arguments
Python del Statement
Python Program to Illustrate Different Set Operations
Node.js vs Python for Backend Development
Python Program to Create a Countdown Timer
Python Program to Reverse a Number
Python Program to Iterate Over Dictionaries Using for Loop
Python Program to Find Sum of Natural Numbers Using Recursion
Python String isalnum()
Python String zfill()
Python ord()
Python while Loop
Python String casefold()
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python str()
Python format()
Python Program to Differentiate Between type() and isinstance()
Python Set remove()
Python Program to Find Numbers Divisible by Another Number
Python abs()