Find ASCII Value of a Character in Python
ord('A') is 65. Type two characters and this program refuses.
BeginnerBasic Python ProgramsExample 4 of 25
find-ascii-value-of-a-character.py
1# Program to find the ASCII value of a character23ch = input("Enter a character: ")45if len(ch) != 1:6 print("Please enter exactly one character.")7else:8 print("The ASCII value of", ch, "is", ord(ch))
Output
Enter a character: A The ASCII value of A is 65
In this one
- ord('A') → 65, ord('a') → 97
- input() can be more than one character. Check len.
What's going on
ord('A') is 65. ord('a') is 97. Same function, Unicode code point — for English letters it matches old ASCII.
len(ch) != 1 is there because input() lets people type AB and then you are debugging a string, not a character.
ord('') crashes. Empty input is the other footgun.
After this program
If the idea is still fuzzy, read Day 1: Introduction to Python in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 25 basic python programs, or see every topic on Python exercises.