Many times in Python programming while doing some projects. We need to find the corresponding Unicode value of a character. So the Python ord function returns the number representing the Unicode code of a specified character.
The Python ord Function is an inbuilt function which returns an integer representing the Unicode code of the specified character. In other words, in Python, every Unicode character has been assigned an integer number. So by using Python ord() built-in function, we can find out that integer number. By directly passing the Unicode character to this function as an argument.
Before discussing anything else, it is important to first know what the Unicode encoding is
Unicode is a character encoding standard that has widespread acceptance. Microsoft software uses Unicode at its core. Whether you realize it or not, you are using Unicode already! Basically, “computers just deal with numbers. They store letters and other characters by assigning a number for each one. Before Unicode was invented, there were hundreds of different encoding systems for assigning these numbers. No single encoding could contain enough characters.”
If you are using a legacy encoding your font conflicts with the font someone in another area of the world uses. You might have a Ű in your font while someplace else someone used a Ɛ at the same codepoint. Your files are incompatible. Unicode provides a unique number for every character and so you do not have this problem if you use Unicode. If your document calls for U+0289 it will be clear to any computer program what the character should be.
Syntax of Python ord() Function
The syntax of Python
|c||Any character of length 1.||Required||String|
Return value of Python ord() Function
The return value is an integer number that uniquely corresponds to or simply represent the provided Unicode Character.
Python ord Function Examples
Let’s move directly to the examples and see the working of the ord() function.
Example 1: Converting a char to it’s corresponding ASCII / Unicode value.
In the below example, the Python ord() function is used to return the number representing the Unicode code of a specified character.
print(ord('H')) print(ord('a')) print(ord('9')) print(ord('#'))
72 97 57 35
So in the above example, we get the Unicode value of the corresponding character.
Note: We can pass only one character as the parameter. The syntax can be ord(“a”) or ord(‘a’), both will give the same results.
Example 2: Finding the Unicode Value of an Empty String
So in this example, we are trying to find the Unicode / ASCII value of an empty string. Many of you guys may don’t know that there is a Unicode value of an empty string too.
In this example, we have taken an empty string. And assigned it to x.
Further, with the help of Python ord() Function, we are finding the Unicode value of the empty string.
x = ' ' print(ord(x))
Example 3: When String length is more than one
The Python ord() method raises an exception when the string length is more than one.
# inbuilt function return an # integer representing the Unicode code # demonstrating exception value1 = ord('AB') # prints the unicode value print (value1)
Traceback (most recent call last): File "c:/Users/Karan/Desktop/test.py", line 5, in <module> value1 = ord('AB') TypeError: ord() expected a character, but string of length 2 found
If the string length is more then one, and a TypeError will be raised.
Example 4: The Unicode points of numbers 0-9 by using range and Python ord
#Know Unicode code points of numbers by ord() function for n in range(10): print("Unicode code point of", n, '=', ord(str(n)))
Unicode code point of 0 = 48 Unicode code point of 1 = 49 Unicode code point of 2 = 50 Unicode code point of 3 = 51 Unicode code point of 4 = 52 Unicode code point of 5 = 53 Unicode code point of 6 = 54 Unicode code point of 7 = 55 Unicode code point of 8 = 56 Unicode code point of 9 = 57
In the above example, a range of 10 numbers (0-9) is created. A for loop is used to iterate through the range. The ord Python function is used to display the code point of each number in the range.
Example 5: Return Unicode code Point Value From a User Input
For this example, the character is taken by the user by using the input function. Enter a single character and ord() function will return the Unicode code point of that character. Have a look:
py_var = input("Enter a character:") py_codepoint = ord(py_var) print("The Unicode code point of the character",py_var ,"=" ,py_codepoint)
Enter a character:K The Unicode code point of the character K = 75
Example 6: Python Program to Get the Unicode code Point Value of a File Using Python ord
In this example, we are using Python ord() Function in a File. Here we will first open the file in the read-only mode. Then we will initialize a for loop and get the Unicode of all the characters present in the file.
import sys filename = sys.argv with open(filename) as fh: content = fh.read() for c in content: print(ord(c))
Example 7: Program to Return Unicode Values of all the Elements Present in the List
The Python ord() method can be used with iterables containing characters as elements.
MyList = ['P', 'y', 't', 'h', 'o', 'n', 'P', 'o', 'o', 'l'] for i in MyList: print(ord(i))
80 121 116 104 111 110 80 111 111 108
In this example, we get all the corresponding Unicode values of all the elements present in the list.
Example 8: Using List comprehension and ord() function in Python to remove all characters other than alphabets.
The ord() function accepts a character as an argument and returns back the corresponding ASCII value. This allows us in easy and quick comparison.
Here we also implemented list comprehension which allows us to filter all the necessary elements of a list and club them together with the help of join function to get the desired output.
def remchar(input): # checking uppercase and lowercase characters final = [ch for ch in input if (ord(ch) in range(ord('a'),ord('z')+1,1)) or (ord(ch) in range(ord('A'),ord('Z')+1,1))] return ''.join(final) if __name__ == "__main__": input = "[email protected]_/?" print (remchar(input))
- How to Convert String to Lowercase in
- How to Calculate Square Root
- User Input | Input () Function | Keyboard Input
- Best Book to Learn Python in 2020
Python ord function takes a single character as an input or a string of length 1 and returns the integer i.e Unicode equivalent code. The computer only understands these Unicode codes, Unicode codes are converted into binary which is used by the computer to process information.
Python ord is something we need to know to effectively use Python. We can change characters by adding or subtracting offset values.
Try to run the programs on your side and let me know if you have any queries.