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()
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    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 FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer