Python Program to find ASCII value of a character or digit entered by the user

Program

ch = raw_input("Enter a character:\t")
print "The ASCII value of '" + ch + "' is ", ord(ch)

Python Program to Find ASCII Value of a Character or Digit Entered by the User

Explanation of ASCII

ASCII (American Standard Code for Information Interchange) is a character encoding system that assigns a unique numeric value to each character, digit, or symbol.

Examples:

  • 'A' → 65
  • 'a' → 97
  • '0' → 48
  • '@' → 64

ASCII values are widely used in text processing, sorting, and data transmission.

Python Program

  1. User Input (raw_input())

    ch = raw_input("Enter a character:\t")
    
    • raw_input() takes user input as a string (used in Python 2).
    • The user enters a single character or digit.
  2. Finding ASCII Value (ord())

    print "The ASCII value of '" + ch + "' is ", ord(ch)
    
    • ord(ch) returns the ASCII value of the entered character.
    • print displays the output.

Key Takeaways

  • ASCII assigns numeric codes to characters and digits.
  • ord() returns the ASCII value of a character.
  • Python 2 uses raw_input(), while Python 3 uses input().

Output

Enter a character:	r
The ASCII value of 'r' is  114