This tutorial explains what the Python Ord() function is, when, and how to use it with full code examples. It simply converts a character into its corresponding Unicode code point. Check out the next section to learn more details about this function.
This function has been part of Python since its early versions. Similar functions are available in other languages such as getchar()
and putchar()
in C. It supports a variety of use cases that you’ll find in the example section.
Must Read: Multiline string in Python with Examples
What is the Python Ord() function?
The ord() function takes a single character which could be of any type that Python supports. Once called, it provides the Unicode code point of the given character. The Unicode code point is a number that represents the character in the Unicode character encoding system. Unicode is a standard that assigns a unique code point to every character in the world, regardless of the language or platform you’ll use it.
Syntax
The ord()
function requires the following syntax:
ord(character) -> int
Parameter | Description |
---|---|
character | A single character (a string of length 1) to find the Unicode code point. |
Return Value | An integer representing the Unicode code point of the input character. |
Possible Errors | TypeError occur if the input character is not a single character (e.g., a string with a length greater than 1). |
Also Read: Iterate String in Python with Examples
How to Use the Ord() Function in Python Code
To use the ord()
function, simply pass the character or string you want to get the code point of as an argument. For example, to get the code point of the letter A
, you would write:
Python code:
>>> ord('A')
65
This will return the integer 65
, which is the Unicode code point for the letter A
.
You can also use the Python ord()
function to get the code points of multiple characters at once. To do this, simply pass a string containing the characters you want to get the code points of to the ord()
function. For example, to get the code points of the "Hello, world!"
string, you would write:
Python code:
>>> ord('Hello, world!')
72101108101111211132
This will return the 72101108101111211132
integer, which is the Unicode code point for each character in the string.
Check This: String Reverse in Python Using For Loop
Ord() Function Examples
The ord()
function in Python is useful for a variety of advanced tasks, such as:
- Checking if a character is a letter, number, or other type of character.
- Converting characters to and from their Unicode code points.
- Encrypting and decrypting data.
- Working with text in different languages.
- Creating custom algorithms for processing text.
Here are some examples of advanced use cases for the ord()
function:
Example#1 Checking if a character is a letter, number, or other type of character.
Python code:
def is_letter(char):
return ord(char) >= 65 and ord(char) <= 90 or ord(char) >= 97 and ord(char) <= 122
def is_number(char):
return ord(char) >= 48 and ord(char) <= 57
def is_punctuation(char):
return ord(char) in [33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 58, 59, 60, 61, 62, 63, 64, 91, 92, 93, 94, 95, 96, 123, 124, 125, 126]
Example#2 Converting characters to and from their Unicode code points.
Python code:
def char_to_code_point(char):
return ord(char)
def code_point_to_char(code_point):
return chr(code_point)
Try This: Append to String in Python with Examples
Example#3 Using the Ord() function for encrypting and decrypting data in Python.
Python code:
def encrypt(string, key):
encrypted_string = ''
for char in string:
encrypted_string += chr(ord(char) + ord(key))
return encrypted_string
def decrypt(encrypted_string, key):
decrypted_string = ''
for char in encrypted_string:
decrypted_string += chr(ord(char) - ord(key))
return decrypted_string
Example#4 Working with text in different languages.
Python code:
# Get the Unicode code point of the letter `你好` in Chinese.
>>> ord('你好')
25953
Example#5 Using the Ord() function to write custom algorithms in Python for processing text.
Python code:
# Create a custom algorithm to remove all punctuation from a string.
def remove_punctuation(string):
new_string = ''
for char in string:
if not is_punctuation(char):
new_string += char
return new_string
# Example usage:
>>> remove_punctuation('Hello, world!')
'Helloworld'
These are just a few examples of the many advanced ways that the ord()
function can be used.
Sure. Here are some additional advanced topics and examples of how to use the ord()
function in Python.
Unicode Code Points and Code Pages
Unicode code points are unique numbers that represent characters in the Unicode character encoding system. Code pages are tables that map Unicode code points to characters.
The ord()
function returns the Unicode code point of a given character, but it does not tell you what code page the character is encoded in. To determine the code page of a character, you can use the encoding
attribute of the str
object.
For example, to get the code page of the string "Hello, world!"
, you would write:
Python code:
>>> 'Hello, world!'.encoding
'utf-8'
This tells us that the string is encoded in the UTF-8 code page.
Working with bytes
A byte is a unit of data consisting of eight bits. Bytes are a binary representation of characters.
The Python ord()
function can also be used to get the Unicode code point of a byte. To do this, you must first convert the byte to a character using the chr()
function.
For example, to get the Unicode code point of the byte b'A'
, you would write:
Python code:
>>> ord(chr(b'A'))
65
This will return the integer 65
, which is the Unicode code point for the letter A
.
Also Check: Python Regular Expression Examples
Working with regular expressions
Regular expressions are a powerful tool for searching and manipulating text. The ord()
function can be used to create Python regular expressions that match characters based on their Unicode code points.
For example, to create a regular expression that matches all letters, you would write:
Python code:
>>> re.compile('[a-zA-Z]')
This regular expression will match any character with a Unicode code point between 65 and 90 (inclusive) or between 97 and 122 (inclusive).
Conclusion
The ord()
function is a powerful tool that can be used for a variety of tasks, including checking character types, converting characters to and from Unicode code points, encrypting and decrypting data, working with text in different languages, and creating custom algorithms for processing text.
We hope this tutorial has been helpful. Please let us know if you have any questions.
Happy coding!