Notes

`ord()` Function in Python [ English ]

< Prev Next >

1. Introduction

The ord() function in Python is a built-in function used to convert a character into its corresponding Unicode (ASCII) integer value. In simple terms, it returns the numeric code representing a given character.

Every character in a computer system is stored internally as a numeric value. The ord() function helps convert that character into its Unicode code point.

2. Syntax

ord(character)

Parameter

3. Return Value

4. Basic Example

print(ord('A'))

Output

65

Explanation

5. Example with Lowercase Character

print(ord('a'))

Output

97

Explanation

6. Example with a Digit

print(ord('5'))

Output

53

Explanation

7. Example with Special Character

print(ord('$'))

Output

36

Explanation

8. Example with Unicode Character

print(ord('☃'))

Output

9731

Explanation

9. Relationship Between ord() and chr()

Function Purpose
ord() Converts a character to its Unicode number
chr() Converts a Unicode number to its character

Example:

print(ord('A'))
print(chr(65))

Output

65
A

10. Practical Example

text = "Python"

for ch in text:
    print(ch, ord(ch))

Output

P 80
y 121
t 116
h 104
o 111
n 110

Explanation

11. Summary

< Prev Next >