| by Arround The Web | No comments

Python ascii() Function

ASCII refers to the American Standard Code for Information Exchange, as it is a standard way of encoding text as numbers, symbols, and characters on many devices. ASCII codes range from “0” to “127”, each representing an English character. In Python, the “ascii()” function is used to retrieve the printable representation of any object.

This guide will present you with a detailed tutorial on the Python “ascii()” function via the following contents:

What is the “ascii()” Function in Python?

In Python, the “ascii()” function is a built-in function that takes strings, lists, tuples, or any objects and retrieves the readable version by escaping any non-ASCII characters with escape characters. For example, the “å” will be replaced by “\xe5”.

Syntax

ascii(object)

 

Parameters

In the above-provided syntax, the “object” parameters specify any Python object, such as lists, strings, dictionaries, tuples, etc.

Return Value

The “ascii()” function retrieves an object string with non-ASCII characters escaped.

Example 1: Using Python “ascii()” Function to Escape non-ascii Characters

The below code defines a variable with an ASCII string value. Next, the “ascii()” function converts the input string into its corresponding ASCII character codes. In Python 3, strings are Unicode by default, so we need to use the “ascii()” function to convert them to ASCII characters before printing:

str1 = ascii("Welcome to Jåva Guide")
print(str1)

 

In the below output, all the non-ASCII characters have been escaped:

Example 2: Using Python “ascii()” Function with Lists

In the below code, initially, we create a list of strings with non-ASCII characters. Next, the “ascii()” function is applied to the lists and retrieves the printable representation of an object. This method escapes any non-ASCII chatterers using backslashes:

list1 = ['pythØn','Jåva','C++']
print(ascii(list1))

 

As you can see, the non-ASCII characters have been escaped successfully:

Example 3: Using Python “ascii()” Function with Set and Tuple

In the below code, the set and tuple object are created with multiple non-ASCII characters. After that, the “ascii()” method is used to retrieve the printable representation by escaping non-ASCII characters with “\u” or “\x” prefixes:

set1 = {'å', 'Φ', 'η'}
tuple1 = ('ö', 'Ð','ß','√', '¶')
print(ascii(set1), '\n')
print(ascii(tuple1))

 

The execution of the above code retrieves the printable representation consisting of ASCII characters and non-ASCII characters. The non-ASCII characters are replaced with escape characters that start with “\u” and “\x” hexadecimal followed by their code:

Example 4: Retrieving the Printable Carriage Return Character in a String

In this code, the “ascii()” method is applied to the multiline string. The “ascii()” method retrieves the new string by replacing the new line with the “\n” escape character:

str1='''Hello
and welcome to Python Guide.'
''
print(ascii(str1))

 

After executing the above code, the below output will be shown:

Conclusion

The built-in “ascii()” function in Python retrieves the string that escapes the non-ASCII characters in the string using “\x”, and “\u” escapes. The function replaces all the non-ASCII characters of the passed objects to the function with escape characters. This guide demonstrated a detailed tutorial on the Python “ascii()” function using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply