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 Generate a Random Number
Python Dictionary pop()
Python Program to Catch Multiple Exceptions in One Line
Python Program to Compute all the Permutation of the String
Python hash()
Python Functions
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python Data Types
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python String isdigit()
Python bytes()
Python String encode()
Python any()
Python hasattr()
Python Set intersection()
Python Type Conversion and Type Casting
Python Program to Merge Mails
Python Program to Concatenate Two Lists
Python String istitle()
Python break and continue
Python Recursion
Python Program to Print all Prime Numbers in an Interval
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python String lower()
Python Program to Randomly Select an Element From the List
Python Program to Make a Simple Calculator
Python slice()
Python Program to Count the Number of Each Vowel
Python Program Read a File Line by Line Into a List
Python Program to Extract Extension From the File Name
Python sum()
Python Set copy()