Home » Python ord() and chr() Functions Working for Unicode

Python ord() and chr() Functions Working for Unicode

python ord and chr functions

In programming, it’s common to work with characters and strings, but when it comes to handling non-ASCII characters, things can get a bit more complicated. That’s where the ord() and chr() functions in Python come in handy. These built-in functions allow you to work with Unicode code points and characters in a straightforward way. In this article, we’ll explore the syntax and usage of the ord() and chr() functions in Python and compare them with their counterparts in other top programming languages.

Understanding the ord() Function in Python

The ord() function in Python is used to print the Unicode code point of a single character. In other words, it returns an integer representing the Unicode code point of a character. The syntax of ord() is simple: you only need to pass a single character as an argument. For example, if you want to find the Unicode code point of the letter “A”, you can use the following code:

This will output the integer value 65, which is the Unicode code point of the letter “A”. It’s important to note that the ord() function only works with a single character, not a string. If you try to pass a string of length greater than 1 to ord(), you’ll get a TypeError.

Using the chr() Function in Python

The chr() function in Python is the opposite of the ord() function. It takes an integer representing a Unicode code point as an argument and returns the corresponding character. For example, if you want to get the character corresponding to the Unicode code point 65, you can use the following code:

This will output the letter “A”. Like the ord() function, chr() only works with a single integer argument representing a Unicode code point. If you pass a non-integer value or an integer that doesn’t represent a valid Unicode code point, you’ll get a ValueError.

ord() and chr() in Other Top Programming Languages

While ord() and chr() are Python built-in functions, many other programming languages have similar functions for working with Unicode code points and characters. Here are some examples:

JavaScript

In JavaScript, the charCodeAt() function is equivalent to Python’s ord() function, and the fromCharCode() function is equivalent to Python’s chr() function.

Java

In Java, the equivalent of Python’s ord() function is the charAt() function, and the equivalent of Python’s chr() function is the valueOf() function.

Ruby

In Ruby, the ord() function works the same way as in Python, but the chr() function is named chr(Encoding::UTF_8) and takes an additional argument specifying the character encoding.

PHP

In PHP, the equivalent of Python’s ord() function is the ord() function, and the equivalent of Python’s chr() function is the chr() function.

C#

In C#, the equivalent of Python’s ord() function is the Convert.ToChar() function, and the equivalent of Python’s chr() function is the Convert.ToInt32() function.

Go

In Go, the equivalent of Python’s ord() function is the int() function, and the equivalent of Python’s chr() function is the string() function.

The Final Say

In this article, we’ve covered the basics of the ord() and chr() functions in Python, and compared them to their equivalents in other top programming languages. These functions are essential when working with Unicode characters and code points, and understanding how they work can make your code more efficient and robust. Whether you’re a beginner or an experienced programmer, the ord() and chr() functions are important tools to have in your toolkit.

Ilyas Ozkurt

Hello I'm İlyas Özkurt. I am a software developer who has been working on websites for 10 years. My first studies in this field were during my high school years. Now I work as a software developer at 6harf and am responsible for all web projects there.

Add comment