ord() and chr() Function in Python

# ord() and chr() Function in Python

Vikram Singh
Assistant Manager - Content
Updated on Jun 14, 2024 14:47 IST

In this article, we will learn how to use ord() and chr() functions in Python with the help of examples. These functions are used to convert unicode to integer and integer to unicode respectively.

ord () and chr() functions of Python are built-in functions that are used to convert a character to an integer and vice-versa. Before starting the article, we will discuss Unicode, which we will use later in our discussion.
Unicode is an internationally recognized character encoding system that assigns each character a unique number, which helps the programmer to create a string with different characters of different languages (including special characters and emojis). The Unicode standards include numbers from 0 to 1,114,111.

Example:

1. The unicode for 0 is 481 is 49, β¦., 9 is 57
2. The unicode for a is 97b is 98, β¦. z is 122.

Now, without further delay, letβs move to learn more about ord() and chr() functions and how to use them.

## What is the ord() function in Python?

ord() function in Python is used to convert a single Unicode character into its integer representation, i.e., it takes a single string character as an input and returns an integer (representing the Unicode equivalent of the character) as an output.

• ord() function returns an error if the length of the string is not equal to 1.

#### Syntax

`ord(ch)Copy code`

Parameter

The ord() function takes only a single character:

ch: a character whose Unicode value is required.

Return Value

int: returns an integer value that is equivalent to the Unicode value of the provided character.

Now, letβs have a look at some examples to get a better understanding.

### How to find the Unicode using ord() function in Python?

```# use ord() function to find the Unicode of UpperCase_Char =ord( 'A')lowercase_char = ord('a')Special_Char = ord('\$') num = ord('9') print('The Unicodes are:', UpperCase_Char, lowercase_char, Special_Char, num)Copy code```

Output

### How to find the Unicode of each alphabet in the String?

`# use ord() function to find the unicode of each alphabet in the stringname = 'InfoEdge'for i in name: print(ord(i))Copy code`

Output

### How to find the Unicode of string of length greater than one?

`#use ord() function for the string length gretaer than 1.name = ord('Vikram')print(name)Copy code`

Output

Note: In above example, ord() function returns an error, since it takes only a single character.

## What is chr() function in Python?

As we have discussed above, the ord() function takes a character and returns the corresponding Unicode, and the chr() function does exactly the opposite to the ord() function, i.e., it takes Unicode (a number between 0 and 1,114,111) and returns corresponding character.

Syntax

`chr(i)Copy code`

where,
i: is an integer that represents the Unicode of the character.

Now, letβs take some examples to get a better understanding of how to use chr() function in Python.

### How to find characters against the Unicode using chr() function?

```# use chr() function to find the characters corresponding to the given Unicode num = [65, 97, 36, 57]for i in num: print(chr(i))Copy code```

Output

From the above example, we get how to find the character against the give Unicode, but what if we give the value to chr() function not lying in the range 0 β 1,114,111.

Letβ see the below example.

```# use chr() function to find the charcter#chr() function considers the values between 0 and 1,114,111 print(chr(-19))print(chr(1,114,120))Copy code```

Output

## Conclusion

In this article, we have discussed how to use ord() and chr() functions of python with the help of exmaples.

Hope you will like the article.

Keep Learning!!

Keep Sharing!!

Top Trending Article

Interview Questions

## FAQs

What is the ord() function in Python?

ord() function in Python is used to convert a single Unicode character into its integer representation, i.e., it takes a single string character as an input and returns an integer (representing the Unicode equivalent of the character) as an output.

What is chr() function in Python?

As we have discussed above, the ord() function takes a character and returns the corresponding Unicode, and the chr() function does exactly the opposite to the ord() function, i.e., it takes Unicode (a number between 0 and 1,114,111) and returns corresponding character.

What is Unicode?

Unicode is a standard that assigns a unique numerical value (code point) to every character, regardless of the platform, program, or language. It encompasses a vast range of characters from various writing systems and symbols used worldwide.

Can ord() and chr() functions handle non-Unicode characters?

No, the ord() and chr() functions specifically deal with Unicode characters. They are not designed to work with characters from non-Unicode character sets like ASCII.

Can the ord() and chr() functions be used interchangeably?

Yes, you can use the ord() function to obtain the code point for a character and then pass that code point to the chr() function to obtain the original character again. This makes ord() and chr() functions complementary to each other.