ord()

ord() Unicode point of input char

ord() takes input char of single length.
returns Unicode point value of input Char.

Here are some sample outputs of single input string ( output is shown against each row after comment )
print(ord('a')) # 97
print(ord('b')) # 98
print(ord('P')) # 80
print(ord('#')) # 35
print(ord('$')) # 36
The first 128 unicode point values are same as ASCII values.

If we input more than one char then it will return error.
print(ord('ab'))
TypeError: ord() expected a character, but string of length 2 found

Sample Script

Converts any lower case char to upper case char ( without using any built-in string functions )
str='Welcome to Python'
for i in str:
  k=ord(i) # ASCII value of i
  if (k >=97 and k<=122):
    print(chr(k-32),end='')
  else:
    print(i,end='')
Output
WELCOME TO PYTHON

Example 1: Converting a String to Unicode Points

for char in 'hello':
    print(f"'{char}': {ord(char)}")
Output
'h': 104
'e': 101
'l': 108
'l': 108
'o': 111

Example 2: Using ord() with Non-ASCII Characters

print(ord('€'))  # Output: 8364
print(ord('😊'))  # Output: 128522

Difference between ord() and chr() in Python

ord(): Converts a character into its corresponding Unicode code point (an integer).

print(ord('A'))  # Output: 65

chr(): Converts a Unicode code point (an integer) into its corresponding character.

print(chr(65))  # Output: 'A'

ord() and chr() are inverse functions, commonly used for character-to-integer and integer-to-character conversions in Python.


All Built in Functions chr() returns the char by taking the Unicode point value ascii()
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer