← All coding practice

Coding Practice · Beginner · October 12, 2026 · 5 min read

Python Coding Exercise #35: Alphabetically Last

Part of the series Python Practice Exercise — part 35

Before you start: Know input() and if, elif and else · Know comparison operators

The question

Python comparison operators can also be used on strings. String a is smaller than string b if it comes alphabetically before b. Notice however that the comparison is only reliable if

  • the characters compared are of the same case, i.e. both UPPERCASE or both lowercase
  • only the standard English alphabet of a to z, or A to Z, is used.

Please write a program which asks the user for two words. The program should then print out whichever of the two comes alphabetically last.

You can assume all words will be typed in lowercase entirely.

Some examples of expected behaviour:

Please type in the 1st word: car
Please type in the 2nd word: scooter
scooter comes alphabetically last.
Please type in the 1st word: zorro
Please type in the 2nd word: batman
zorro comes alphabetically last.
Please type in the 1st word: python
Please type in the 2nd word: python
You gave the same word twice.

My Solution

word1 = input("Please type in the 1st word: ")
word2 = input("Please type in the 2nd word: ")
if word1 < word2:
    print(f"{word2} comes alphabetically last.")
elif word2 < word1:
    print(f"{word1} comes alphabetically last.")
elif word1 == word2:
    print("You gave the same word twice.")

Notice the full stop at the end of comes alphabetically last. The sample output has one, and in an exercise that checks the output exactly, a missing full stop counts as a wrong answer.

How My Solution Works

1. Strings can be compared

You already know < and > for numbers. Python also lets you use them on strings, and then they mean “comes before” and “comes after” in alphabetical order:

print("car" < "scooter")   # True, because c comes before s
print("zorro" < "batman")  # False, because z comes after b

So word1 < word2 asks: “does the first word come before the second one alphabetically?” If so, the second word is the one that comes last.

2. Three outcomes again

As in the earlier if / elif exercises (Exercise #33 and Exercise #34), there are three possibilities, and each has its own branch:

  • word1 < word2: the first word comes first, so print word2.
  • word2 < word1: the second word comes first, so print word1.
  • word1 == word2: the words are identical, so print the “same word” message.

Here are the three examples:

1st word2nd wordword1 < word2?word2 < word1?word1 == word2?Printed
carscooteryes(skipped)(skipped)scooter comes alphabetically last.
zorrobatmannoyes(skipped)zorro comes alphabetically last.
pythonpythonnonoyesYou gave the same word twice.

Watch the direction. The smaller word is the one that comes first, so the word to print is the other one, the bigger one.

University Solution

The University Solution turns the comparison around, so the word that gets printed is the one that’s greater, and it ends with a plain else:

word1 = input("Please type in the 1st word: ")
word2 = input("Please type in the 2nd word: ")

if word1 > word2:
    print(word1, "comes alphabetically last.")
elif word2 > word1:
    print(word2, "comes alphabetically last.")
else:
    print("You gave the same word twice.")

Using > means the word being tested is also the word being printed, which is slightly easier to follow: “if word1 is greater, print word1”. And the last else works because, if neither word is greater, the words must be equal. The output is exactly the same as in My Solution. (The comma in print(word1, "comes...") adds the space between the word and the text; see Exercise #05.)

How Python compares strings

Python compares two strings one character at a time, from the left. It finds the first position where they differ and compares those two characters. Behind the scenes, every character has a number (its character code), and the characters are compared by that number:

print(ord("a"), ord("b"), ord("z"))   # 97 98 122

Letters of the alphabet come in order, so a is smaller than b, which is smaller than z. That’s what makes alphabetical order work. Here are some details that follow from this rule:

A shorter word that matches the start comes first. If one word is the beginning of the other, the shorter one is smaller, just like in a dictionary:

print("car" < "cart")   # True

So for car and cart, the program prints cart.

The first difference decides. For cat and car, the first two letters match, and the third letters differ: t against r. Since t comes after r, cat is the greater word. The rest of the word doesn’t matter.

The empty string is smallest. "" < "a" is True.

Why the exercise says “all lowercase”

The exercise warns that the comparison is only reliable if both words use the same case. This is because uppercase letters have smaller character codes than lowercase ones:

print(ord("A"), ord("Z"))   # 65 90
print(ord("a"), ord("z"))   # 97 122

All the capital letters come before all the lowercase letters. So Python thinks "Zebra" comes before "apple":

print("Zebra" < "apple")   # True, even though z comes after a in the alphabet

With mixed case, the program would give the wrong answer. If you need to handle capital letters, convert both words to lowercase before comparing:

print("Zebra".lower() < "apple".lower())   # False, which is correct

The .lower() method (see String Methods in Python) returns a lowercase copy of the text. The exercise tells you to assume lowercase, so you don’t need it here, but it’s worth remembering. The second condition in the exercise matters too: letters with accents, such as é, have codes far beyond z, so they don’t sort the way a dictionary would.

Another way to find the last word

max() and min(), which you met in Exercise #23, also work on strings, using the same alphabetical rule:

print(max("car", "scooter"))   # scooter

That finds the word that comes last in one step. But the exercise needs a different message for two identical words, and max("python", "python") just returns python. So you would still need an if to catch the equal case.

Key takeaways

  • The comparison operators < and > work on strings. a < b means “a comes alphabetically before b”.
  • Python compares strings from the left, one character at a time, and the first difference decides.
  • A word that is the start of another word comes first: "car" < "cart".
  • Uppercase letters come before lowercase ones, so "Zebra" < "apple" is True. Use .lower() on both words if the case might be mixed.
  • The smaller word comes first alphabetically. To print the word that comes last, print the larger one.
  • A missing full stop in the printed text counts as a wrong answer when the output must match exactly.

✦ 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 · elif