Python ord() function in Linux

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.

Here at LinuxAPT, as part of our Server Management Services, we regularly help help our Customers to perform related Python functions queries.

In this context, we shall look into how to use the ord() function in Python.


More about Python ord() function

The ord() function has a function that returns the Unicode representing the specified argument.

It's syntax is given below:

ord(character)

ord() Parameter Values is:

character: any word, string


Examples of using Python ord() function

1. Let's take a look at the below ord() function:

x = ord("b")

print(x)

The Output will be:

98


2. Basic ord() function:

x = ord("B")
print(x)

It's Output will be:

66


3. Enter a word and print out the Unicode of that word:

print("Enter the word:")
x = input()
y = ord(x)
print("Unicode:",y)

It's Output will be:

Enter the word: t
Unicode: 116


4. The program will be an error if the string length is not equal to 1:

x = ord('BC')
print(x)

It's Output will be:

---------------------------------------------------------------------------
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


5. Take a look at this ord() fuction:

print(ord('5'))    # 53
print(ord('A'))    # 65
print(ord('$'))    # 36

It's Output will be:

53
65
36


[Need assistance in fixing Python function issues ? We can help you. ]

This article covers how to use the ord() function in Python. In fact, The ord() function (short of ordinal) returns an integer representing the character passed to it. For ASCII characters, the returned value is 7-bit ASCII code, and for Unicode characters, it refers to the Unicode code point.

Related Posts