Program To Print Ascii Value of a Character

1. Introduction

The American Standard Code for Information Interchange (ASCII) is a character encoding standard for electronic communication. ASCII codes represent text in computers, telecommunications equipment, and other devices that use text. Each letter, digit, or symbol is assigned a unique ASCII code. Python provides a simple way to find the ASCII value of characters using the ord() function. This blog post will guide you through writing a Python program to print the ASCII value of a character entered by the user.

2. Program Steps

1. Prompt the user to enter a single character.

2. Use the ord() function to find the ASCII value of the entered character.

3. Display the ASCII value to the user.

3. Code Program

# Step 1: Prompt the user to enter a single character
char = input("Enter a character: ")

# Step 2: Use the ord() function to get the ASCII value of the character
ascii_value = ord(char)

# Step 3: Display the ASCII value
print(f"The ASCII value of '{char}' is {ascii_value}")

Output:

Enter a character: A
The ASCII value of 'A' is 65

Explanation:

1. The program starts by asking the user to input a single character. This character is stored in the variable char.

2. To find the ASCII value of the entered character, the ord() function is used. This built-in Python function takes a string of length one (a single character) and returns an integer representing the Unicode code point of the character. For ASCII characters, this is essentially the ASCII value.

3. The program then prints out the character along with its ASCII value using a formatted string, showing the user the ASCII value of the character they entered.


Comments