Skip to Content

Python Basics: How Do You Find the Next Alphabetical Character in Python Using ord() and chr()?

What Happens When You Combine Python’s ord() and chr() Functions to Manipulate Characters?

Learn the relationship between characters and their Unicode values in Python. Understand how the ord() function converts a character to an integer and chr() converts it back, allowing you to perform arithmetic on characters, such as finding the next letter in the alphabet.

Question

What is the result of chr(ord(‘a’) + 1)?

A. ‘b’
B. ‘a’
C. ‘A’
D. ‘c’

Answer

A. ‘b’

Explanation

ord(‘a’) is 97; 97+1 = 98; chr(98) is ‘b’.

The expression chr(ord(‘a’) + 1) evaluates to ‘b’.

This expression demonstrates the relationship between characters and their underlying numerical representation (Unicode code points). The process occurs in three steps:

  1. ord(‘a’): The ord() function takes a single character and returns its integer Unicode code point. For the lowercase letter ‘a’, its ASCII/Unicode value is 97.
  2. 97 + 1: The expression then adds 1 to this integer, resulting in 98.
  3. chr(98): The chr() function performs the inverse operation of ord(). It takes an integer code point and returns the corresponding character. The character for the code point 98 is ‘b’.

This technique is a common way to perform character-based arithmetic, such as iterating through letters of the alphabet.

The other options are incorrect because:

  • ‘a’ would be the result if 0 were added instead of 1.
  • ‘A’ is an uppercase letter with a different Unicode value (65).
  • ‘c’ would be the result of chr(ord(‘a’) + 2).

Python Basics: Learn, Apply & Build Programs certification exam assessment practice question and answer (Q&A) dump including multiple choice questions (MCQ) and objective type questions, with detail explanation and reference available free, helpful to pass the Python Basics: Learn, Apply & Build Programs exam and earn Python Basics: Learn, Apply & Build Programs certificate.