Python ascii() Function

This is a detailed tutorial of Python ascii() Function. Learn to convert any Python Object into a printable string representation by escaping non-ASCII characters.

Python ascii() Function

The ascii() function that comes built-in with Python is used to convert any Python Object into a printable string representation. If the object to be converted contains any non-ASCII characters, it will escape them by using different escapes. (\x, \u or \U)

Let’s say you wanted a string object that contains ASCII as well as NON-ASCII characters to be converted into a printable representation string. Then this function will convert it such that all of the ASCII characters will remain as it is while all of the Non-ASCII characters will be converted into escape codes. You’ll have clarity of this while having a look at the examples and their outputs.

Syntax

ascii()

This function takes a Python Object as an argument. You can provide any other objects other than strings as well. (For example, a List, etc.)

It will return the printable representation of the provided object as an argument to this function as a Python String.

The ASCII characters such as A, a, b, B, 0, 1, 2.. etc. will remain as it is.

The Non-ASCII characters will be escaped using the \x, \u or \U escapes. For example, the Spanish character, ñ is non-ASCII and hence it will be converted into its corresponding escape character, i.e. \xf1

Examples

The following two examples illustrate how you can make use of the ascii() built-in method for various practical applications where conversion to ASCII format is required.

Example 1. Working of ascii() Method with Strings

allASCIIString = "Hello, this is a simple English String!"
print(ascii(allASCIIString))

mixString = "1 is written as १ in Pure Hindi."
print(ascii(mixString))

In the above example, we’ve taken two different strings. The first string is written in all English characters that are included in the ASCII characters. Therefore this function returns the given string as it is. On the other hand, the second string contains a non-ASCII character i.e. a Unicode Character . Therefore, this function converted it into its corresponding Unicode code, i.e. \u0967.

Python Ascii() Function Example

Example 2. Working of ascii() Function with different Python Objects

Here we’re providing the argument to the ascii() function that belongs to the different Data Types, other than string.

aList = [1, 2, "Thréé", '५', 5]
aDictionary = {"key1" : "value1", "key2" : '२००'}
aTuple = ("ਲਾਲ", "Green", "Blue")

print(ascii(aList))
print(ascii(aDictionary))
print(ascii(aTuple))

print(type(ascii(aList)))
print(type(ascii(aDictionary)))
print(type(ascii(aTuple)))

In the above code, we’re providing a List, a Dictionary, and a Tuple as an argument to this function. These python objects contain data that further contains some non-ASCII characters. In the output, you can clearly observe that all of the non-ASCII characters are converted into their corresponding escape codes. But the return type of this function is always a String. To show this, I’ve especially printed the type for the return value using the type() method.

Python Ascii() Built In Function Different Python Objects And Data Types Example

I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.

Leave a Reply

Your email address will not be published. Required fields are marked *