HomePythonPython Program to Find the ASCII Value of a Character

Python Program to Find the ASCII Value of a Character

In this python tutorial, you will learn how to find the ASCII Value of a character using the ord function, print function and input function of the python programming language.

The ASCII is short form  for American Standard Code for Information Interexchange, ASCII is a standard that assigns letters, numbers, and other characters in the 256 slots available in the 8-bit code.

How to Find the ASCII Value of a Character in Python?

Let’s take a look at the first source code, here the character is assigned in the code and the ord function is carried out.

RUN CODE SNIPPET
# Program to find the ASCII value of the given character

c = 'g'
print("The ASCII value of '" + c + "' is", ord(c))

OUTPUT:

The ASCII value of 'g' is 103

  • At the start, we declare the variable  c  and assign it with the character 'g' .
  • Now, we display the output value using the print and display the statement/string as ""The ASCII value of '" + c + "' is", ord(c).
  • In the above statement, the variables "+ c +" will hold the ASCII value where the ord(c) function converts the given string of length one and returns an integer representing the unicode code point of the character. In simpler terms, it converts a character to an integer.

Let’s take a look at the second source code , here the character is given as input by the user in the code and the ord function is carried out.

RUN CODE SNIPPET
# Program to find the ASCII value of the given character

c = input('Enter the character: ')
print("\n The ASCII value of '" + c + "' is", ord(c))

INPUT:

%

OUTPUT:

Enter the character: 
The ASCII value of '%' is 37

  1. Here we give the user the option to enter the values and the input values are scanned using the input function which is  declared with the variable  c .
  2. In the STDIN section of the code editor the input values are entered.
  3. Then we display the output value using the print and display the statement/string ""The ASCII value of '" + c + "' is", ord(c).
  4. As given in the first program, the variables "+ c +" will hold the ASCII value where the ord() function converts a character to an integer.

NOTE:

  1. The ord function converts a character into its Unicode code value. This method accepts a single character and it consists of alphabets and symbols.
  2. The print statement is followed by a comma, to initiate the ord function.
  3. The print statement/string to be displayed in enclosed in double quotes.
  4. The statement for the input function are enclosed in single quotes and parenthesis.
  5. The \n in the code indicates a new line or the end of a statement line or a string.
  6. For the ord function to be carried, the ‘c’ variable is enclosed in + on both ends within double quotes. At the end of the print statement, it will hold the assigned variable (ord(c)).

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...
This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...
This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...