Part of the series

Several example codes

~1 min read • Updated Oct 1, 2025

Program Overview

This Python program reads a single character from the user and displays its ASCII code.
The character is stored in the variable ch, and the ord() function is used to convert it to its numeric ASCII value.


Python Code:


# Read a character from the user
ch = input("Enter a single character: ")

# Check that only one character was entered
if len(ch) != 1:
    print("Please enter exactly one character.")
else:
    # Calculate ASCII code
    ascii_code = ord(ch)

    # Display result
    print("\n--- Result ---")
    print(f"Entered character: {ch}")
    print(f"ASCII code: {ascii_code}")

Sample Output:


Enter a single character: A  

--- Result ---  
Entered character: A  
ASCII code: 65

Step-by-Step Explanation:

- The user enters a single character
- The program checks that the input is exactly one character
- The ord() function converts the character to its ASCII code
- The result is printed in a structured format


Written & researched by Dr. Shahin Siami