Tutorial

Python ord(), chr() functions

Published on August 3, 2022
author

Pankaj

Python ord(), chr() functions

Python ord() and chr() are built-in functions. They are used to convert a character to an int and vice versa. Python ord() and chr() functions are exactly opposite of each other.

Python ord()

Python ord() function takes string argument of a single Unicode character and return its integer Unicode code point value. Let’s look at some examples of using ord() function.

x = ord('A')
print(x)

print(ord('ć'))
print(ord('ç'))
print(ord('$'))

Output:

65
263
231
36

Python chr()

Python chr() function takes integer argument and return the string representing a character at that code point.

y = chr(65)
print(y)
print(chr(123))
print(chr(36))

Output:

A
{
$
ć

Since chr() function takes an integer argument and converts it to character, there is a valid range for the input. The valid range for the argument is from 0 through 1,114,111 (0x10FFFF in hexadecimal format). ValueError will be raised if the input integer is outside that range.

chr(-10)

Output:

ValueError: chr() arg not in range(0x110000)

Let’s see an example of using ord() and chr() function together to confirm that they are exactly opposite of another one.

print(chr(ord('ć')))
print(ord(chr(65)))

Output:

ć
65

That’s all for a quick introduction of python ord() and chr() functions.

You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: Official Documentation - ord, Official Documentation - chr

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
January 12, 2022

mega nützliche tipp danke bro

- milo

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    May 23, 2020

    Thank you. You explain things very clearly.

    - Neil Thomas

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      March 26, 2020

      Thank you very much!

      - Vishal Vijaykumar Parkar

        Try DigitalOcean for free

        Click below to sign up and get $200 of credit to try our products over 60 days!

        Sign up

        Join the Tech Talk
        Success! Thank you! Please check your email for further details.

        Please complete your information!

        Become a contributor for community

        Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

        DigitalOcean Documentation

        Full documentation for every DigitalOcean product.

        Resources for startups and SMBs

        The Wave has everything you need to know about building a business, from raising funding to marketing your product.

        Get our newsletter

        Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

        New accounts only. By submitting your email you agree to our Privacy Policy

        The developer cloud

        Scale up as you grow — whether you're running one virtual machine or ten thousand.

        Get started for free

        Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

        *This promotional offer applies to new accounts only.