In this post, we will write a Python program to find ASCII value of a character with a detailed explanation and example but before we jump into the programming part let’s understand what is ASCII value and why we use it.

What are ASCII values and why do we use them?

ASCII stands for “American Standard Code for Information Interchange.
It is like a set of rules that computers use to understand and display text and some special symbols. Each letter, number, or symbol on your keyboard has a unique number assigned to it in this set of rules.

For example, the letter “A” is represented by the number 65, and the number “1” is represented by the number 49.

These rules help computers communicate and display text on screens or print it on paper.

Now we know what are ASCII values and why we use them, So it is time to dive into our main aim, which is to find the ASCII value of a given character in Python.

Find ASCII Value of a Character in Python

To find the ASCII value of a character in Python, there is a built-in function called the ord() function that takes a single character as input and returns its ASCII value.

Source Code

Programming concepts you have to know before writing this program:

char = input("Enter your character: ")

ascii_value = ord(char)
print(f"ASCII value of '{char}' is : {ascii_value}")

Output-1

Enter your character: A
ASCII value of 'A' is : 65

Output-2

Enter your character: 1
ASCII value of '1' is : 49

For practice, let’s do a few more Python programs based on ASCII values so that we can brush up on a few more concepts related to Python.

Few more Programs for Practice

Write a Python program to print ASCII values for A to Z characters

Before writing this program, few programming concepts you have to know:

Source Code

my_chars = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

all_ascii = {i: ord(i) for i in my_chars}

for c,a in all_ascii.items():
    print(f"{c}: {a}")

Output

A: 65
B: 66
C: 67
D: 68
E: 69
F: 70
G: 71
H: 72
I: 73
J: 74
K: 75
L: 76
M: 77
N: 78
O: 79
P: 80
Q: 81
R: 82
S: 83
T: 84
U: 85
V: 86
W: 87
X: 88
Y: 89
Z: 90

Write a Python program to print the ASCII value of 0 to 9 numbers

Before writing this program, few programming concepts you have to know:

Source Code

all_ascii = {i: ord(str(i)) for i in range(0, 10)}

for c,a in all_ascii.items():
    print(f"{c}: {a}")

Output

0: 48
1: 49
2: 50
3: 51
4: 52
5: 53
6: 54
7: 55
8: 56
9: 57

Author

Hi, I'm Yagyavendra Tiwari, a computer engineer with a strong passion for programming. I'm excited to share my programming knowledge with everyone here and help educate others in this field.

Write A Comment