Register Login

Python ord() Functions

Updated Oct 22, 2019

Introduction

ord() function is an inbuilt function in python which takes a single unicode character as an argument and returns its equivalent integer unicode code value (ASCII value). 

For example: ord(A) will return 65 (ASCII value of 'A')

Syntax

ord(x)

where x is a single Unicode character

Parameter 

x String, any character

Return Value

Unicode code value or ASCII value of input character

Example of ord() function in Java

#Pyton code to get the ASCII Value of inserted character

#Ord method with the Exception Handling

try:
  inputCharacter = input("Enter a Character / Number : ")
  ordValue = ord(inputCharacter)
  print("ASCII VALUE IS : ",ordValue)
except TypeError as e:
  print(e)

OUTPUT:

Enter a Character / Number : 55
ord() expected a character, but string of length 2 found

Enter a Character / Number : 5
ASCII VALUE IS :  53 

ord () Exception Handling

#Pyton code to get the ASCII Value of inserted character

#Ord method with the Exception Handling

try:
  inputCharacter = input("Enter a Character / Number : ")
  ordValue = ord(inputCharacter)
  print("ASCII VALUE IS : ",ordValue)
except TypeError as e:
  print(e)

OUTPUT::

Enter a Character / Number : 55
ord() expected a character, but string of length 2 found

This exception occurs when the input character is of two digit


×