×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python ord() Function: Use, Syntax, and Examples

By IncludeHelp Last updated : December 07, 2024

Python ord() function

The ord() function is a library function in Python, it is used to get an integer representing the Unicode character from given character value, it accepts a character and returns an integer i.e., it is used to convert a character to an integer or it is used to get the ASCII value (Unicode value) of a given character.

Consider the below example with sample input/output values:

Input:
val = 'A'
print(ord(val))
    
Output:
65

Syntax

The following is the syntax of ord() function:

ord(character)

Parameter(s):

The following are the parameter(s):

  • character – character value to be converted in an integer value.

Return Value

The return type of ord() function is <class 'int'>, it returns an integer value of given character.

Python ord() Example 1: Convert character to integer / ASCII code of the character

# python code to demonstrate an example
# of ord() function

val = "A"
print("ASCII code of", val, "is =", ord(val))

val = "x"
print("ASCII code of", val, "is =", ord(val))

val = "@"
print("ASCII code of", val, "is =", ord(val))

val = "8"
print("ASCII code of", val, "is =", ord(val))

Output

ASCII code of  A  is =  65
ASCII code of  x  is =  120
ASCII code of  @  is =  64
ASCII code of  8  is =  56

Python ord() Example 2: Print ASCII codes of the string characters

# python code to demonstrate an example
# of ord() function

# string
x = "Hello, World!"

# Printing ASCII code of each character
for i in x:
    print(i, ord(i))

Output

H 72
e 101
l 108
l 108
o 111
, 44
  32
W 87
o 111
r 114
l 108
d 100
! 33


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.