Remember
ord() returns the Unicode code point of a single character. It does not accept a string containing several characters.
ord() returns the Unicode code point of a single character. It does not accept a string containing several characters.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
character = 'A'
print(ord(character))Top-level scope
'A'ord() returns the Unicode code point of a single character. It does not accept a string containing several characters.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
character = 'A'
print(ord(character))
|
v
DISPLAYED RESULT
65
FINAL BINDINGS
character --> object 1 (str)
object 1: 'A'Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
character = 'A'
print(ord(character))Top-level scope
'A'The example has finished. Compare the final output with the values and references above.
65
What do you expect this program to print?
character = 'A'
print(ord(character))ord() FunctionThe ord() function in Python returns the Unicode code point (an integer) for a given character. It is the reverse of the chr() function.
ord(character)
character – A string representing a single Unicode character.print(ord('a'))
97
print(ord('A'))
65
print(ord('@'))
64
print(ord('₹')) # Rupee symbol
8377
This works with any character in the Unicode standard, not just English letters.
ord('ab') will raise a TypeError.ord('') will also raise a TypeError.The ord() function is often used in problems that involve character frequency, shifting characters (like in Caesar cipher), or sorting letters.
ord() converts a character to its Unicode code point.Write a program that takes a character as input and prints its Unicode value.
ch = input("Enter a character: ")
if len(ch) == 1:
print("Unicode code point is:", ord(ch))
else:
print("Please enter exactly one character.")
Connect what you noticed in the example with the idea below.
ord() returns the Unicode code point of a single character. It does not accept a string containing several characters.
ord() accepts any length of text.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.