Python ascii() function with example

Python ascii() function  is Python built-in function ascii() function return the object after converting all the non-ascii characters in some printable text  in that object.

Syntax of ascii() function

 

ascii(object_name)

 

  • ascii is function name.
  • object_name  is that object on which ascii function has to apply.
  • ascii function will return the object in printable state in which all the non-ascii characters are converted to printed values e.g. something like  \t\xa3.

 

Example of Python ascii function

Let’s understand Python ascii() function taking few examples.

 

Example

#Python ascii() function

str="hi pound:£"

print(ascii(str))

Output

'hi pound:\xa3'

 

Explanation

  • Here str is the String containing pound sign (£) as non-ascii character.
  • when we apply ascii function on str, it convert  pound sign (£) to some text \xa3

 

Python ascii function has assign each non ascii character to some value, as we saw Python assign pound (£) to \xa3 . below are some outputs of non ascii characters when we apply ascii function on them

 

#Python ascii() function

p="£"
y="¥"
e="€"
c="©"
r="®"
tm="™"

print(ascii(p))
print(ascii(y))
print(ascii(e))
print(ascii(c))
print(ascii(r))
print(ascii(tm))

 

Output

'\xa3'
'\xa5'
'\u20ac'
'\xa9'
'\xae'
'\u2122'

 

Explanation

As shown above Python convert all above non ascii characters to some printable text.

In nutshell, ascii function convert the non ascii characters to some printed readable text.

 

Some non ascii characters

Below are few non ascii characters, you can try to apply ascii function on these:

 

®
§ ° ·
±
× ÷ ¼
½f ¾ μ
π
READ  Python chr() function
Please follow and like us:

One Comment to “Python ascii() function with example”

  1. […] Python ascii() function with example […]

Comments are closed.