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 theord(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
- 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 variablec
. - In the STDIN section of the code editor the input values are entered.
- Then we display the output value using the
print
and display the statement/string""The ASCII value of '" + c + "' is", ord(c).
- As given in the first program, the variables
"+ c +"
will hold the ASCII value where theord()
function converts a character to an integer.
NOTE:
- The ord function converts a character into its Unicode code value. This method accepts a single character and it consists of alphabets and symbols.
- The print statement is followed by a comma, to initiate the ord function.
- The print statement/string to be displayed in enclosed in double quotes.
- The statement for the input function are enclosed in single quotes and parenthesis.
- The \n in the code indicates a new line or the end of a statement line or a string.
- 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)).