chr() function and ord() function in Python

By | March 29, 2020

Topic: chr() function and ord() function in Python

Using chr and ord functions in Python

What is the Use of chr() function and ord() function in Python

chr() function in Python

The chr() function in Python takes an integer Ascii code and returns the character (actually a string of one character)at that Ascii code value. For example , chr(65) will return ‘A’ and chr(97) will give ‘a’.

Note that the argument must be in the range [0..255], inclusive; Otherwise, ValueError will be raised if i is outside that range.

ord() function in Python

ord() function takes a string of length one (single character) as an argument and returns an integer representing the Unicode code point of that character when the argument is a unicode object, or the value of the byte when the argument is an 8-bit string. For example, ord(‘A’) gives 65 and ord(‘a’) returns the integer 97.

Note that ord() function is the inverse of chr() for 8-bit strings. Similarly ord() function is the inverse of unichr() function for unicode objects. In Python a unicode argument can take the value from 0 to 65535 otherwise a typeError will be raised.

You will also like to read:

Python Programs usin chr()

Loading

Leave a Reply

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