← All coding practice

Coding Practice · Beginner · October 15, 2026 · 6 min read

Python Coding Exercise #41: Alphabetically in the Middle

Part of the series Python Practice Exercise — part 41

Before you start: Know input() and if, elif and else · Know that strings can be compared alphabetically

The question

Please write a program which asks the user for three letters. The program should then print out whichever of the three letters would be in the middle if the letters were in alphabetical order.

You may assume the letters will be either all uppercase, or all lowercase.

Some examples of expected behaviour:

1st letter: x
2nd letter: c
3rd letter: p
The letter in the middle is p
1st letter: C
2nd letter: B
3rd letter: A
The letter in the middle is B

My Solution

letter1 = input("1st letter: ")
letter2 = input("2nd letter: ")
letter3 = input("3rd letter: ")
if (letter2 < letter1 < letter3) or (letter3 < letter1 < letter2):
    print(f"The letter in the middle is {letter1}")
elif (letter1 < letter2 < letter3) or (letter3 < letter2 < letter1):
    print(f"The letter in the middle is {letter2}")
else:
    print(f"The letter in the middle is {letter3}")

How My Solution Works

1. Letters can be compared

Strings can be compared with < and >, and then the comparison follows alphabetical order: "c" < "p" is true, because c comes before p. (This was explained in Exercise #35.) That’s what makes this exercise possible without any special tools.

2. The middle letter is between the other two

The letter in the middle is the one that comes after one of the other letters and before the other. In a chained comparison, that’s easy to write:

letter2 < letter1 < letter3

This means “letter1 is greater than letter2 and less than letter3”, so letter1 is between the two. (Chained comparisons were used in Exercise #36 and Exercise #38.)

But a letter can be in the middle in two ways: the other two letters can be in either order. letter1 is in the middle if letter2 < letter1 < letter3 or letter3 < letter1 < letter2. That’s why each branch has two chained comparisons joined with or. (The brackets aren’t required, but they make the two halves easy to see.)

3. The three branches

  • If letter1 is between the other two, print letter1.
  • Otherwise, if letter2 is between the other two, print letter2.
  • Otherwise, print letter3.

The last branch is an else with no condition. If neither of the first two letters is in the middle, the third one has to be.

Here is every order of the letters a, b and c, and the branch each one uses:

1st2nd3rdBranch that matchesPrinted
abcsecond (letter1 < letter2 < letter3)b
acbelseb
bacfirst (letter2 < letter1 < letter3)b
bcafirst (letter3 < letter1 < letter2)b
cabelseb
cbasecond (letter3 < letter2 < letter1)b

In all six orders, the middle letter is b, and My Solution finds it. The same goes for the exercise’s examples: for x, c, p, the letter p is between c and x, so it’s the one printed. My Solution gives the correct answer for every possible set of three different letters, uppercase or lowercase.

University Solution

letter1 = input("1st letter: ")
letter2 = input("2nd letter: ")
letter3 = input("3rd letter: ")

if letter1 > letter2 and letter1 > letter3:
    if letter2 > letter3:
        middle = letter2
    else:
        middle = letter3
elif letter2 > letter3:
    if letter3 > letter1:
        middle = letter3
    else:
        middle = letter1
else:
    if letter2 > letter1:
        middle = letter2
    else:
        middle = letter1

print("The letter in the middle is " + middle)

The University Solution takes a different approach. It’s longer, and it uses nested if statements (an if inside another if) and a variable called middle.

  1. The first if checks whether letter1 is the largest of the three. If it is, the middle letter has to be the larger of the other two, so the nested if compares letter2 with letter3 and stores the bigger one in middle.
  2. The elif is reached when letter1 isn’t the largest. It checks whether letter2 is greater than letter3. If so, letter2 is greater than letter3, and the nested if decides whether the middle is letter3 or letter1.
  3. The else covers the remaining case, where letter3 is the largest or tied for it, and the nested if compares letter2 with letter1.

In every branch, the program first works out which letter is the biggest, and then picks the larger of the other two. That larger one is the middle letter.

The result is stored in middle, and one print() at the end shows it. The text is joined with + instead of an f-string, which works here because middle is already a string.

Both solutions are correct for all letters that are different from each other. My Solution is shorter, but the University Solution never has to repeat letters in long conditions, and it only uses simple one-step comparisons.

A difference: repeated letters

The exercise doesn’t say what happens if the user types the same letter more than once. But it’s an interesting edge case, because the two solutions don’t agree on it. In alphabetical order, the letters a, a, b are a, a, b, so the middle letter is a.

1st2nd3rdMiddle (alphabetical)My SolutionUniversity Solution
aababa
baaaaa
abbbbb
cccccc

For a, a, b, My Solution prints b. None of its conditions can be true when two letters are equal, because < is strict, so the program falls through to the else and prints the third letter. The University Solution gives the right answer in all the cases above. Since the exercise only asks for three letters in general, either one is fine here, but it’s a good reminder to think about repeated values.

A shorter way: sorted()

Python can sort the letters, and then the middle one is simply the second item of three:

letter1 = input("1st letter: ")
letter2 = input("2nd letter: ")
letter3 = input("3rd letter: ")
letters = sorted([letter1, letter2, letter3])
print(f"The letter in the middle is {letters[1]}")

sorted() returns a new list with the letters in alphabetical order, and letters[1] is the item at position 1, the middle of three. (Lists and positions are covered in Core Data Structures in Python, sorted() in Built-in Functions in Python, and sorting was used in Exercise #02.) It gives the correct answer in every case, including repeated letters. The exercise is meant to practise if statements, so the comparison solutions are the intended ones, but sorted() is the shortest.

Why the exercise says “all uppercase or all lowercase”

As in Exercise #35, the comparison only works as alphabetical order when the letters have the same case. Capital letters come before all lowercase letters in Python’s ordering. To ignore the case, see String Methods in Python. So for the letters a, B and C, the alphabetical middle is B, but both programs print C, because they sort the capitals first: B, C, a. The exercise avoids this by promising that the case will be the same.

Key takeaways

  • Strings, including single letters, can be compared with < and > in alphabetical order.
  • A chained comparison such as letter2 < letter1 < letter3 means “letter1 is between the other two”.
  • A letter can be in the middle in two ways, because the other two can be in either order, so each case needs two chained comparisons joined with or.
  • A final else can take the last remaining case, when the other cases have been ruled out.
  • A nested if is an if inside another if. It’s longer, but each step is a single simple comparison.
  • Repeated values are an edge case worth testing. < and > are strict, so equal letters can slip past the conditions.
  • sorted() plus [1] finds the middle of three in one line.
  • Comparing letters of different case doesn’t follow alphabetical order, so the exercise promises the letters are all the same case.

✦ This is part of my Python Practice Exercise series, where I work through exercises while learning Python and explain each answer in plain language. ✦


Practice Source: This exercise is taken from the Python Programming MOOC 2026 by the University of Helsinki. I’m working through the exercises as part of my Python learning journey and documenting my answers and explanations here.

Tagged: python · strings · string-comparison · conditionals · nested-if