Python ascii() function is Python built-in function. ascii() function returns the object after converting all the non-ascii characters in some printable text in that object.
Syntax of ascii() function
- ascii is the function name.
- object_name is that object on which ascii function has to apply.
- ascii function will return the object in a 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 by taking few examples.
#Python ascii() function str="hi pound:£" print(ascii(str))
- Here str is the String containing the pound sign (£) as non-ascii character.
- when we apply ascii function on str, it converts the pound sign (£) to some text \xa3
Python ascii function has assigned 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 to 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))
'\xa3' '\xa5' '\u20ac' '\xa9' '\xae' '\u2122'
As shown above, Python converts all above non-ascii characters to some printable text.
In nutshell, ascii function converts 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: