Signup/Sign In
LAST UPDATED: APRIL 25, 2023

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

Technology #Python

    Hi Geeks, In this tutorial we are going to have a look on chr() and ord() Function in Python 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 Function in Python

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


    #1. chr() Function in Python:

    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() Function -

    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() Function in Python

    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() Function-

    ord( c ) 
    # c must be an string

    This method takes only a parameter as input and the parameter must be a 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 Function in Python. If you feel that this post is useful, please share it with your friends and colleagues.

    Thanks for reading it till the end.

    Frequnetly Asked Questions(FAQs)

    1. What is Ord () function in Python?

    The ord() function in Python is used to convert a character to its corresponding ASCII (or Unicode) integer value.

    2. What is CHR () in Python with example?

    The chr() function in Python is used to convert an integer value to its corresponding character based on the ASCII (or Unicode) value.

    3. What is the difference between CHR and Ord?

    The main difference between chr() and ord() functions in Python is that chr() converts an integer value to its corresponding character, while ord() converts a character to its corresponding integer value.

    4. Why is it called Ord in Python?

    The name "ord" is short for "ordinal," which refers to the position of a number in a series. In Python, the ord() function is used to find the numeric representation (position) of a character in the ASCII (or Unicode) table.

    I am a self-taught Blogger and Programmer. I have good sort of knowledge in Technology, PHP, Javascript, HTML, CSS and Python.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS