Python Coding Exercise #55: The Longer String
Part of the series Python Practice Exercise — part 55
Before you start: Know input() · Know len() · Know if, elif and else
The question
Please write a program which asks the user for two strings and then prints out whichever is the longer of the two - that is, whichever has the more characters. If the strings are of equal length, the program should print out “The strings are equally long”.
Some examples of expected behaviour:
Please type in string 1: hey
Please type in string 2: hiya
hiya is longer
Please type in string 1: howdy doody
Please type in string 2: hola
howdy doody is longer
Please type in string 1: hey
Please type in string 2: bye
The strings are equally long
Before coding: compare the lengths, not the strings
The question is “which has more characters”, so the program has to compare the lengths of the strings, not the strings themselves. The function len() gives the length, as in Exercise #30:
len("hey") # 3
len("hiya") # 4
Comparing the strings directly would answer a different question. As in Exercise #35, < and > on strings compare alphabetical order, so "hey" > "hiya" would say which comes later in the alphabet, and not which is longer.
There are three possible outcomes, and each one needs its own branch:
| Situation | What the program prints |
|---|---|
| String 1 is longer | String 1, then is longer |
| String 2 is longer | String 2, then is longer |
| Both have the same length | The strings are equally long |
My Solution
word1 = input("Please type in string 1: ")
word2 = input("Please type in string 2: ")
word1_length = len(word1)
word2_length = len(word2)
if word1_length > word2_length:
print(f"{word1} is longer")
elif word2_length > word1_length:
print(f"{word2} is longer")
else:
print("The strings are equally long")
How My Solution Works
1. Read the two strings
The two input() lines store what the user types in word1 and word2. They stay as strings, because strings are what the program compares.
2. Store the two lengths
word1_length = len(word1) and word2_length = len(word2) put the lengths in variables. This way, the if and elif below can compare two short names instead of two function calls.
3. if/elif code block
if word1_length > word2_length:runs when string 1 is longer, and prints string 1.elif word2_length > word1_length:runs when string 2 is longer, and prints string 2.else:runs when neither is longer. If string 1 isn’t longer and string 2 isn’t longer, the lengths must be equal, so theelseneeds no condition of its own.
The f-string puts the longer string and the words is longer into one message.
Following the three samples
| String 1 | String 2 | Lengths | Branch | Python prints |
|---|---|---|---|---|
hey | hiya | 3 and 4 | elif | hiya is longer |
howdy doody | hola | 11 and 4 | if | howdy doody is longer |
hey | bye | 3 and 3 | else | The strings are equally long |
University Solution
input_string1 = input("Please type in string 1: ")
input_string2 = input("Please type in string 2: ")
if len(input_string1) > len(input_string2):
print(input_string1, "is longer")
elif len(input_string2) > len(input_string1):
print(input_string2, "is longer")
else:
print("The strings are equally long")
The University Solution has the same three branches, with two small differences:
- It calls
len()inside the conditions. There are no variables for the lengths, so each condition callslen()itself. My Solution stores the lengths first, which makes the conditions shorter, and each length is calculated only once. - It prints with a comma.
print(input_string1, "is longer")puts a space between the string and the text, and gives the same output as my f-string. (See Exercise #05 for how commas and spaces work inprint().)
Both programs print identical output for the three samples and for every other input tested.
Things to notice
Spaces are characters. howdy doody has 11 characters, because the space between the two words counts as one. That’s why it is longer than hola, which has 4. The same applies to spaces at the start or the end of a string.
The strings only need to be equally long, not equal. hey and bye are different words, but both have 3 characters, so the program prints The strings are equally long. The else branch doesn’t check whether the text is the same.
An empty string has length 0. If the user presses Enter without typing anything for one of the strings, its length is 0, so the other string is longer, unless it is empty too. If both are empty, the program prints The strings are equally long.
The else is the “equal” case. There is no elif word1_length == word2_length. Since the first two branches have already covered “longer” and “shorter”, only “equal” is left.
Key takeaways
len()gives the number of characters in a string, and spaces count as characters.- To compare lengths, compare
len()of the strings. Comparing the strings with<or>compares alphabetical order. - With three outcomes, an
if, anelifand anelsecover all the cases, and theelseis the one left over. - Storing a value in a variable, like
word1_length, keeps a condition short. Callinglen()directly in the condition gives the same result. - A comma in
print()and an f-string can print the same text.
✦ 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.