ord() and chr() Function in Python
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.
Explore How to Find an Armstrong Number Using Python
Example:
- The unicode for 0 is 48, 1 is 49, β¦., 9 is 57.
- The unicode for a is 97, b is 98, β¦. z is 122.
Code Point Range | Class |
0-31 | non-printable character (smiling face, suit, bullet, circle, male sign, female sign, etc.) |
32-64 | punctuation, symbols, numbers, and space |
65-90 | uppercase English alphabet ( A-Z) |
91-96 | [, \\, ], ^, _, ` |
97-122 | lowercase English alphabet (a-z) |
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)
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)
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))
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)
Output
Note: In above example, ord() function returns an error, since it takes only a single character.
Best-suited Python courses for you
Learn Python with these high-rated online courses
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)
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))
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))
Output
Recursion Function in Python | count() Function in Python |
len() Function in Python | float() Function in Python |
range() Function in Python | lambda() Function in Python |
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
Top Online Python Compiler | How to Check if a Python String is Palindrome | Feature Selection Technique | Conditional Statement in Python | How to Find Armstrong Number in Python | Data Types in Python | How to Find Second Occurrence of Sub-String in Python String | For Loop in Python |Prime Number | Inheritance in Python | Validating Password using Python Regex | Python List |Market Basket Analysis in Python | Python Dictionary | Python While Loop | Python Split Function | Rock Paper Scissor Game in Python | Python String | How to Generate Random Number in Python | Python Program to Check Leap Year | Slicing in Python
Interview Questions
Data Science Interview Questions | Machine Learning Interview Questions | Statistics Interview Question | Coding Interview Questions | SQL Interview Questions | SQL Query Interview Questions | Data Engineering Interview Questions | Data Structure Interview Questions | Database Interview Questions | Data Modeling Interview Questions | Deep Learning 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.
Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio