Methods and Functions

Python ascii()

Before understanding what Python ASCII means, let us first look at what ASCII represents. In our daily lives, we make use of alphabets, numbers, and special characters. These are none other than ASCII.

ASCII stands for “American Standard Code for Information Interchange“. It is a 7-bit character encoding system that represents English characters with numbers ranging from 0 to 127. It includes numbers ranging from 0-9, Upper case alphabets (A-Z), Lower case alphabets (a-z), and some special characters.

For example, Letter A represents number 65 in the ASCII system, letter Z represents number 90. Lower case alphabet a is represented by the number 97. Special characters such as $ are denoted as 36, @ is denoted by 64 in the ASCII system.

We also have few character set encodings which are different. They are called as non-ASCII character set encodings. Non-ASCII Character Set Encodings are any that are not ASCII (the American Standard Code for Information Interchange). For example, ö, ê, š, ñ, ç, etc.

Definition

  • The Python ascii() function returns a string containing a printable representation of an object for non-alphabets or invisible characters using the \x,\ u, or \U escapes.

Python ascii()

Python ascii() is a built-in function that escapes all the non-ASCII characters in the string and returns a printable representation of an object be it string, tuple, list, dictionary, etc. The syntax of the Python ascii() function is as follows.

Syntax –

where,

object – string, list, tuple, dictionary, etc.

Note – This method can take only one parameter.

Return value from ascii()

The return value of the ascii() function is always a String be it any object passed as a parameter to the function.

Code –

Output –

How ascii() method works?

Example –

                    

# Python program to illustrate ascii() function
txt = 'Hello World'
specialtxt = 'Hêllo Wörld'

print('Text with ASCII characters:', ascii(txt))
print('Text with non-ASCII characters:', ascii(specialtxt))

Output –

                    

Text with ASCII characters: 'Hello World'
Text with non-ASCII characters: 'H\xeallo W\xf6rld'

In the above program, special alphabets which are non-ASCII ‘ê’ and ‘ö’ are converted to \xea and \xf6 respectively after using the ascii() function on the string variable.

Note –

Here, the hexadecimal value of ê in ASCII is EA which is represented using \x prefix, hence, \xea.

Similarly, for ö, the hexadecimal value in ASCII is F6, represented with \x in the prefix, hence, \xf6.

This can also be reversed but we do not use the ascii() function.

Example –

                    

specialtxt = 'H\xeallo W\xf6rld'
print(specialtxt)

Output –

More Examples

Example –

                    

print(ascii('ß'))

print(ascii('ł'))

print(ascii('й'))
 
print(ascii('ø'))

print(ascii('å'))

Output –

                    

'\xdf'

'\u0142'

'\u0439'

'\xf8'

'\xe5'

List as a Parameter to ascii() function

Example –

                    

# List as an input to ascii() function
cars = ['Fõrd', 'āudi', 'Poršhe']

print(ascii(cars))

Ouptut –

                    

['F\xf5rd', '\u0101udi', 'Por\u0161he']

In the above example, we have converted the list into ascii characters. The non-ASCII character is substituted with a readable representation of the non-ASCII character, as shown in the output.

Hence, õ becomes \xf5, ā becomes \u0101, š becomes \u0161.

Tuple as a Parameter to ascii() function

Example –

                    

# Tuple as an input to ascii() function
mytuple = ('√16', 'čat', 'fįsh')

print(ascii(mytuple))

Output –

                    

('\u221a16', '\u010dat', 'f\u012fsh')

In this case, we have a Tuple, mytuple, with three elements that were supplied as a parameter to the ascii() method.

Hence, becomes \u221a, č becomes \u010d and į becomes \u012f.

Frequently Asked Questions

Q1. Can you use ascii in Python?

Python programming language provides a built-in function known as the ascii() function. This ascii() function is used to return a string containing a printable representation of an object for non-ascii characters or invisible characters with \x, \u or \U escapes. The function accepts objects as the parameters that can be either a string, list, tuple, or a dictionary.

Its syntax is

Q2. What is ascii code in Python?

ASCII is a character encoding standard that is used to denote a set of characters with numbers. It is used to represent text format into a numeric format for transmission of data through computers, the internet, telecom equipment, and other such devices. Python provides ascii() function in order to represent the non-ascii characters in a suitable format.

Example –

                    

# Python program to illustrate the use of ascii() function
simple_text = 'Python Programming'
special_text = 'Pÿthøn Programmïng'

print(ascii(simple_text))
print(ascii(special_text))

Output –

                    

'Python Programming'
'P\xffth\xf8n Programm\xefng'

In the above program, the variable ‘special_text’ contains few non-ascii characters which are represented using \x, \u, and \U escapes by using the ascii() function.

Q3. What does chr() do in Python?

Python chr() is a built-in function which accepts numeric value of a certain range and returns their string representation at that code point. For example, in ASCII uppercase ‘A’ represents number 65, lowercase ‘g’ represents 103. Hence the chr() function will return these numeric values for their respective characters. Python chr() function also returns special characters as outputs.

Example –

                    

# Python program to illustrate the use of chr() function
x = chr(65)
y = chr(90)
z = chr(243)

print('ASCII char of 65:', x)
print('ASCII char of 90:', y)
print('ASCII char of 243:', z)

print(chr(9432))
print(chr(12313))

Output –

                    

ASCII char of 65: A
ASCII char of 90: Z
ASCII char of 243: ó
ⓘ
〙

Since chr() function accepts an integer as a parameter, it has a specified range. The function will return output only if the integer satisfies the range of 0 to 11,14,111.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

avatar
  Subscribe  
Notify of

Customize your course in 30 seconds

Which class are you in?
No thanks.