Python program that displays an ASCII character table from ! to ~

Writing a Python Program to Display an ASCII Character Table

Python program that displays an ASCII character table from ! to ~
ASCII (American Standard Code for Information Interchange) is a character encoding standard that assigns unique numbers to each character. In Python, we can use the built-in function chr() to get the character corresponding to an ASCII value. In this blog post, we will write a Python program that displays an ASCII character table from ! to ~. We will display the ASCII value of each character in decimal and hexadecimal and display five characters per line.

Table of Contents

  • What is ASCII?
  • Writing the Program
  • Conclusion

What is ASCII?

ASCII is a character encoding standard that assigns unique numbers to each character. The ASCII table consists of 128 characters, including letters, numbers, punctuation marks, and control characters. Each character is assigned a unique number between 0 and 127.

Writing the Program

Here is the Python program that displays an ASCII character table from ! to ~:

j=1

for i in range(33,127):

    if j%5==0:

        print(chr(i))

    else:

        print(chr(i),end=' ')

    j=j+1

print()

In this program, we use a for loop to iterate over the range of ASCII values from 33 to 126. We use the chr() function to get the character corresponding to each ASCII value.

We use an if statement to print five characters per line. If the current character is a multiple of five away from the starting point, we print a newline character (\n) to start a new line.

The variable j is used to keep track of the number of characters printed so far. If j is a multiple of 5, we print a newline character and reset j to 1. Otherwise, we print the character followed by a space.

| Practice Python MCQ with "Python MCQ Programs Interview  " Android App.

Output:

! " # $ 

% & ' ( ) 

* + , - . 

/ 0 1 2 3 

4 5 6 7 8 

9 : ; < = 

> ? @ A B 

C D E F G 

H I J K L 

M N O P Q 

R S T U V 

W X Y Z [ 

\ ] ^ _ ` 

a b c d e 

f g h i j 

k l m n o 

p q r s t 

u v w x y 

z { | } ~

In conclusion, we have written a Python program that displays an ASCII character table from ! to ~. We have used the built-in functions chr() to get the ASCII value of each character and its corresponding character. We have also displayed each character's ASCII value in decimal and hexadecimal format and displayed five characters per line. This program can be useful for anyone who needs to work with ASCII characters in Python.

| Practical List - Python [ 4330701 ] [ PRACTICAL EXERCISES ] 

FAQ: Python program that displays an ASCII character table from ! to ~

What is the purpose of the end=' ' in the print statement in the program.

- In the given program, the end=' ' parameter in the print statement is used to specify the character that should be printed at the end of each print statement. By default, the print() function in Python ends with a newline character (\n).

- This means that each call to print() will print its output on a new line. However, in this program, we want to print five characters per line. Therefore, we use the end parameter to specify that we want to print a space character (' ') instead of a newline character at the end of each print statement.

- This allows us to print five characters on the same line before starting a new line.

What is the default value of the end parameter in the print function ?

- The default value of the end parameter in the print() function in Python is a newline character (\n).

- This means that each call to print() will print its output on a new line. The end parameter is used to specify the character or string that should be printed at the end of each print statement instead of the default newline character.

- By default, the end parameter is set to \n, but it can be changed to any other character or string as needed.

What is the difference between the end and sep parameters in the print function ?

- The end and sep parameters in the print() function in Python are used for controlling the formatting of the output in different ways.

- The end parameter is used to specify the character or string that should be printed at the end of each print statement instead of the default newline character (\n) .

- By default, the end parameter is set to \n, but it can be changed to any other character or string as needed. On the other hand, the sep parameter is used to specify the separator between the arguments to the print() function

- By default, the separator between arguments is a space character, but it can be modified and made to any character, integer, or string as per our choice using the sep parameter.

- The sep parameter is found only in Python 3.x or later.

| You may like: Python Program to Calculate the Average of N Numbers: A Step-by-Step Guide with Examples

Post a Comment

0 Comments