ord() function in Python converts any Unicode character into a number. It only takes one parameter which must be a single Unicode character, any more than one character produces an error as demonstrated here:
>>> ord('A') 65 >>> ord('ABC') Traceback (most recent call last): File "<input>", line 1, in <module> TypeError: ord() expected a character, but string of length 3 found
Notice how Python informs us of what is expected – a character, but a string was passed in of length 3.
The abbreviation ord is an abbreviation to the popular mathematical term of “ordinal number”. An ordinal number is a number that indicates the position, for example, first, second, third are numbers representing the order of something, perhaps the outcome of a race.
What Is The Reverse Of
The opposing function in Python is the
chr() function, which converts a number into its Unicode character representation.
CHR is an abbreviation of the word character, just as ORD is the abbreviation of ordinal number.
>>> chr(65) 'A' >>> chr(9) '\t' >>> chr(13) '\r'
As shown above Some examples of what the
chr() function returns, including a tab
ord function is a series of ordinal numbers representing an order of Unicode characters, you could find the range of the English alphabet by finding the result of the
ord function for the letter A. However, be aware that
a are two different types of characters – one is uppercase and the other lowercase.
>>> ord('A') 65 >>> ord('Z') 90 >>> ord('a') 97 >>> ord('z') 122
Therefore, the uppercase range of the alphabet starts at 65 and goes through to 90, and the lowercase range of the alphabet starts at 97 and goes through to 122.
Using the same principle above you could also find the range for the ordinal numeric representation of the numbers from 0 to 9. Don’t forget to represent the numbers as strings, otherwise, you will get a
>>> ord('0') 48 >>> ord('9') 57 >>> ord(0) Traceback (most recent call last): File "<input>", line 1, in <module> TypeError: ord() expected string of length 1, but int found
Therefore, 48 to 57 would be the ordinal representation of the numbers from 0 to 9.
How To Use
ord With A String?
The restriction with the
ord function is that it only allows one character in its sole parameter. Therefore, to obtain the ordinal representation of a string you would need to split a string into its individual characters and then loop through each character to obtain the ordinal number.
To do this easily using list comprehensions do the following:
>>> my_ord = "Sw-ord" >>> ord_chars = [my_ord[idx] for idx in range(len(my_ord))] >>> print(ord_chars) ['S', 'w', '-', 'o', 'r', 'd']
As you see the list comprehension allows the break up of each character from the original string to its own element in a list.
Instead of outputting each character into its own list you can change the expression which inserts the character to output instead the
ord result of the character.
Here’s how this would look as a one-liner:
>>> my_ord = "Sw-ord" >>> [ord(my_ord[idx]) for idx in range(len(my_ord))] [83, 119, 45, 111, 114, 100]
The resulting list is the ordinal representation of the Unicode characters in the original string.
Why Even Use
What is the purpose of
ord function and does it have any use case today?
Whether you realise it or not the ord function helps in a major way when it comes to determining how to sort strings.
Think about this for a moment: how would you order every character so that if comparing two strings you could determine which one comes before the other?
Check what happens when you sort this list of characters:
>>> sorted(['y', 'Z', 'a']) ['Z', 'a', 'y']
See the result: the capital
'Z' comes before the lowercase
'a' – why is that?
This is due to the ordinal numbers assigned to each Unicode character. The reason why the capital
'Z' comes first is its ordinal number (90) is less than the ordinal number for the lowercase
So how can you properly sort words regardless of case in Python?
To get around this issue of different cases being placed before their lower case counterparts, as demonstrated above, Python uses a parameter labelled
key which allows you to set the way each property is handled in your list.
This is how the above list would be properly sorted in Python:
>>> sorted(['y', 'Z', 'a'], key=str.lower) ['a', 'y', 'Z']
Another particular use case for the ord function is limiting the characters that users can use when creating usernames especially if they’re publicly displayed.
The first 127 characters of the Unicode set are probably the most familiar to you if you use a standard English keyboard: these characters are defined and codified in the Unicode set and are known as ASCII.
The next 128 characters of the Unicode character set are the Extended ASCII characters, which may be somewhat familiar to you.
However, now that Unicode includes other characters from other languages the issue some websites face (including YouTube and this is why some creators tell their audience if you see a comment that looks like me asking you for money don’t do it!) is that people can use Unicode characters to represent other English letters.
Take for example my name: Ryan. If I place this in my script above I’d get the following output:
>>> username = "Ryan" >>> [ord(username[idx]) for idx in range(len(username))] [82, 121, 97, 110]
But then what about this: Rуаո
What is the ordinal representation of that string? Would you think it the same?
>>> username = "Rуаո" >>> [ord(username[idx]) for idx in range(len(username))] [82, 1091, 1072, 1400]
They’re different, except for the first letter!
So what might appear to be the same letter actually is something different when you use the
This is where this function comes in handy to prevent users from replicating other letters that contain the same appearance as other Unicode characters.
ord() built-in function in Python converts a character into an ordinal integer number. The function only accepts one character at a time and the character must be of string data type.
The ord function is used to help sort strings and can also help web app creators from users create accounts that may look very similar to other users.