CLOSE

   Python  Python Unicode  
   Technology    Programming

Python chr() and ord() Functions - Python Library Functions

         
 SEPTEMBER 25, 2019   by vineelpynam

Hi Geeks, In this tutorial we are going to have a look on chr() and ord() methods. These two functions are opposite to one another. One method returns the character whereas another method returns the Unicode code point. These are very simple and useful methods to learn, right now.

These two methods are very useful when we want to convert a Unicode code point to a character or find the character's Unicode code point.

Chr and Ord Functions

Let's have a look at these functions one by one.




#1. chr() Method:

This method converts a Unicode code point into a character. For example, If you give 8364 as input it returns as output.


Syntax of chr() method

char( i ) 
# i must be an integer

This method takes only parameter as an input and the parameter must be an integer.

The valid range of the integer is from 0 through 1,114,111.

one = chr(100) 
two = chr(110)
three = chr(120)

print(one)
print(two)
print(three)

Output:

d
n
x



#2. ord() Method

This method converts a Character into a Unicode code point. For example, If you give 'P' as input it returns 80 as output.


Syntax of ord() method

ord( c ) 
# c must be an string

This method takes only parameter as an input and the parameter must be an Character(a String).

one = ord('A') 
two = ord('$')
three = ord('~')

print(one)
print(two)
print(three)

Output:

65
36
126



Conclusion

Pheww! This is it. I hope that you enjoyed the post and learned about the chr()and ord() python library functions. If you feel that this post is useful, please share it with your friends and colleagues.

Thanks for reading it till the end.


SHARE YOUR THOUGHTS WITH US!