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.
print(ord('ab'))
TypeError: ord() expected a character, but string of length 2 found
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
for char in 'hello':
print(f"'{char}': {ord(char)}")
Output
'h': 104
'e': 101
'l': 108
'l': 108
'o': 111
print(ord('€')) # Output: 8364
print(ord('😊')) # Output: 128522
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.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.