Introduction
ord() function in Python prints out the Unicode code of a specified character. This function also accepts a string as an argument and prints out the corresponding Unicode code of that string. The Unicode code point is represented by an integer when the value of the variable is a Unicode object or corresponding byte value.
And we will show you how to use the ord() function in Python as you go through it below. Hope you understand.
Example
x = ord("b") print(x)
Output:
98
Definition
The ord() function has a function that returns the Unicode representing the specified argument.
The syntax
ord(character)
Parameter Values:
character: any word, string
More examples
Example 1: Basic ord() function
x = ord("B") print(x)
Output:
66
Example 2: Enter a word and print out the Unicode of that word
print("Enter the word:") x = input() y = ord(x) print("Unicode:",y)
Output:
Enter the word: t Unicode: 116
Example 3: The program will be an error if the string length is not equal to 1
x = ord('BC') print(x)
Output:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Untitled-1 in ----> 1 x = ord('BC') 2 print(x) TypeError: ord() expected a character, but string of length 2 found
Conclusion
Above is the tutorial about how to use the ord() function in Python through examples.
Thanks for reading!