All Posts

Python ord() Function

Python ord() Function

In this tutorial, we will understand the Python ord() Function. We can use the ord function to get the ASCII value of a single character. ASCII (American Standard Code for Information Interchange ) is a character encoding standard for electronic communication. ASCII codes are used to represent text in computers.

Syntax

The general syntax of the function is as follows:

ord(‘<character>’)

The function takes a single string character as an input and returns an integer. i.e., the ASCII value.

For example:

ord(‘A’) will return 65.

Examples

ord(‘A’)
65
ord(“A”)
65

Python ord() Function

 

ord(“TestingDocs”)
Traceback (most recent call last):
File “<pyshell#2>”, line 1, in <module>
ord(“TestingDocs”)
TypeError: ord() expected a character, but string of length 11 found

 

The error is because the ord() function expects a single character as input. The function results in TypeError when the input is a string.

—

Python Tutorials

Python Tutorial on this website can be found at:

https://www.testingdocs.com/python-tutorials/

More information on Python is available at the official website: