Learn and Learn

Learn And Learn - great place for tutorials, references and how-to

Python – Find String Printable of Object – ascii() Function with Examples

ascii() function is used to find the string containing a printable representation of an object in Python for the given input (object – parameter). It uses \x, \u or \U escapes to escape the non-ASCII characters in the string.

ascii() function is a part of Python Built-in Functions (check list)

Syntax of ascii() Function

The syntax of ascii() function in Python is:

 

Parameters of ascii() Function in Python

object – Where object is any object in Python i.e. strings, lists etc. This parameter is required.

 

Compatibility

ascii() function is only available and compatible with Python 3.x.

 

Return Value of ascii() Function in Python

ascii() function returns a string containing a printable representation of an object for the given input (object – parameter). The non-ASCII character will be escaped in string by using \x, \u or \U.
For example, ñ is changed to \xf1 and ß is changed to \xdf.

 

Python ascii() Function Example 1


Output:
'This is my text with unicod\xeb character'
This is my text with unicodë character

 

Python ascii() Function Example 2


Output:

'\xb5 is a micro symbol'
µ is a micro symbol

LearnAndLearn.com © 2018 - All rights reserved