Python Program to print ASCII Value of a Character

In this article, we will learn about python program to print ASCII value of a character entered by user.

Getting Started

We can use ord() function in python to get ASCII value of any character. Then, print the value provided by ord() function using built-in function – print().

We can print ASCII value of character as shown below –

ch = input("Enter any character")
print("ASCII value of " + ch + " is ", ord(ch))

Ouptut:

Enter any character
A
ASCII value of A is  65

In above program,

  • built-in method – input() is used to get input from user. It returns value entered by user.
  • After that, returned value is stored in a new variable – ch.
  • In the next line, ord(ch) returns ASCII value of character – ch.
  • print() method prints the value in specified format.

Above code will work for any character entered by user. For example,
If you enter any character in alphabet as lowercase, ord() function will return value between 97 and 122. Or, if you enter any character in alphabet as uppercase, ord() function will return value between 65 and 90.

That’s how we can write python program to print ASCII value of a character in python.

Learn more about python at – https://tutorialwing.com/python-tutorial/.

Leave a Reply