chr() and ord() in python

Previous
Next

ord():Returns the integer for specified symbol.

chr(): Return the symbol for specified integer value.

Note: To understand the functionality of chr() and ord() functions, we need to know the values of ASCII character set.

ASCII : (Americans Standard Code for Information Interchange)

  • Represents all symbols 1 language using constants
  • The range is 0 – 255
  • A language is at most having 256 symbols.
  • 1 byte range is (0-255)  – 2^8 value
  • Hence we represent a symbol using 1 byte memory.

We can check the values directly at shell window as follows:

>>> chr(65)
'A'
>>> chr(50)
'2'
>>> ord('a')
97
>>> ord('$')
36
>>> ord('1')
49

We can write programs at editor window as follows:

sym = input("Enter symbol :")
num = ord(sym)
print("The value is :", num)

Output:
Enter symbol :$
The value is : 36
num = int(input("Enter number :"))
sym = chr(num)
print("The symbol is :", sym)

Output:
Enter number : 65
The symbol is : A
Previous
Next

Add Comment

Courses Enquiry Form