The Python ord() function is a built-in function that returns the Unicode code point of a given character. It takes a single argument, which can be a string of length 1 or a Unicode character. The returned value is an integer representing the Unicode code point of the character. The ord() function is useful when working with Unicode strings and characters, as it allows you to convert characters to their corresponding code points, which can then be used for various operations such as sorting, searching, and encoding. Keep reading below to learn how to python ord in Kotlin.

Looking to get a head start on your next software interview? Pickup a copy of the best book to prepare: Cracking The Coding Interview!

Buy Now On Amazon

Python ‘ord’ in Kotlin With Example Code

Python’s `ord()` function returns the Unicode code point of a given character. In Kotlin, we can achieve the same functionality using the `toInt()` function.

To get the Unicode code point of a character in Kotlin, we can simply call the `toInt()` function on the character. For example, to get the Unicode code point of the character ‘A’, we can do the following:


val codePoint = 'A'.toInt()

This will set the `codePoint` variable to the integer value `65`, which is the Unicode code point for the character ‘A’.

If we want to get the Unicode code point of a character at a specific index in a string, we can use the `get()` function to retrieve the character at that index, and then call `toInt()` on the character. For example, to get the Unicode code point of the character at index 2 in the string “Hello”, we can do the following:


val codePoint = "Hello".get(2).toInt()

This will set the `codePoint` variable to the integer value `108`, which is the Unicode code point for the character ‘l’.

In summary, to get the Unicode code point of a character in Kotlin, we can simply call the `toInt()` function on the character. If we want to get the Unicode code point of a character at a specific index in a string, we can use the `get()` function to retrieve the character at that index, and then call `toInt()` on the character.

Equivalent of Python ord in Kotlin

In conclusion, the Kotlin programming language provides a similar function to Python’s ord() function called the codePointAt() function. This function returns the Unicode code point of the character at the specified index in a string. It is a useful tool for working with Unicode characters in Kotlin, allowing developers to easily convert characters to their corresponding code points. While the syntax may differ slightly from Python’s ord() function, the functionality is essentially the same. As Kotlin continues to gain popularity among developers, it is important to understand the various functions and tools available in the language, including the codePointAt() function.

Contact Us