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.
# 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}")
Enter a single character: A
--- Result ---
Entered character: A
ASCII code: 65
- 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