BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Python ord() function with examples

Last Updated: March 23, 2019 by Chaitanya Singh | Filed Under: Python Tutorial

The ord() function in Python accepts a string of length 1 as an argument and returns the unicode code point representation of the passed argument. For example ord('B') returns 66 which is a unicode code point value of character ‘B’. In this tutorial, we will discuss the ord() function with the help of examples.

Note: The first 128 Unicode code point values are the same as ASCII

Python ord() function Example

In the following example we are finding the unicode code point values of an integer, a character and a special character. The first 128 unicode code points are same as ASCII values which means the unicode code points are same as the ASCII values of these passed strings of length 1.

# unicode code point of integer
print("The ASCII value of 9 is",ord('9'))

# unicode code point of alphabet
print("The ASCII value of B is",ord('B'))

# unicode code point of special character
print("The ASCII value of ! is",ord('!'))

Output:
Python ord() function example

Another example of Python ord() function

The passed argument must have the length 1, if we try to pass a string with length more than 1 then we will get a compilation error. Lets take an example to see what error we get.

# unicode code point of integer
print("The ASCII value of 10 is",ord('10'))

Python ord() function error

As you can see in the output that we got an error when we passed the string with length greater than 1.

Top Related Articles:

  1. Python chr() function with examples
  2. Python Recursion
  3. Python User defined Functions
  4. Python Keywords and Identifiers with examples
  5. Python Constructors – default and parameterized

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Python Tutorial

Introduction

  • Python Tutorial
  • Python Introduction
  • Install Python
  • PyCharm IDE Installation
  • Python in PyCharm
  • Python Comments
  • Python Variables
  • Python Keywords & Identifiers
  • Python data types

Flow Control

  • Python If
  • Python if..else
  • Python if..elif..else
  • Python Nested If
  • Python for loop
  • Python while loop
  • Python break
  • Python continue
  • Python pass

Python Functions

  • Python Functions
  • Python Recursion

Python Datatypes

  • Python Numbers
  • Python List
  • Python Strings
  • Python Tuple
  • Python Dictionary
  • Python Set

Python OOPs

  • Python OOP
  • Python Class & Object
  • Python Constructors

Python Examples

  • Python Programs

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap