Python Coding Exercise #57: Second and Second to Last Characters
Part of the series Python Practice Exercise — part 57
Before you start: Know input() and len() · Know string indexes, including negative ones · Know if and else
The question
Please write a program which asks the user for a string. The program then prints out a message based on whether the second character and the second to last character are the same or not. See the examples below.
Please type in a string: python
The second and the second to last characters are different
Please type in a string: pascal
The second and the second to last characters are a
Before coding: finding the two characters
As in Exercise #56, every character has an index. Counting from the beginning starts at 0, and counting from the end starts at -1. So:
- The second character has the index
1, so it isword[1]. - The second to last character has the index
-2, so it isword[-2].
For the two samples:
| Word | p y t h o n | p a s c a l |
|---|---|---|
Second character, word[1] | y | a |
Second to last, word[-2] | o | a |
| Same? | No | Yes |
When they are the same, the message also names the character: ... are a.
My Solution
word = input("Please type in a string: ")
character2 = word[1]
character2_last = word[-2]
if character2 == character2_last:
print(f"The second and the second to last characters are {character2}")
else:
print("The second and the second to last characters are different")
How My Solution Works
1. Read the string
word = input("Please type in a string: ") stores what the user typed.
2. Pick out the two characters
character2 = word[1] stores the second character, and character2_last = word[-2] stores the second to last. Storing them in variables gives them names, which makes the if below easy to read.
3. Compare
if character2 == character2_last: is true when the two characters are the same. Then the f-string prints the message with the character in it. Otherwise, the else prints the message with different.
The trap: a string that is too short
The program works for every string that has at least two characters. But a string with fewer than two characters doesn’t have a second character at all:
word = "a"
print(word[1])
IndexError: string index out of range
With the program above, a one-character string like a, or an empty string, makes it stop with this error. The exercise doesn’t test those inputs, but a program that stops with an error is not a good result, so it is worth handling them.
University Solution
word = input("Please type in a string: ")
# Check also that the word is at least two characters long,
# so that the second and second to last characters exist
if len(word) > 1 and word[1] == word[-2]:
print("The second and the second to last characters are " + word[1])
else:
print("The second and the second to last characters are different")
The University Solution solves the same problem with a safety check:
len(word) > 1comes first. It asks whether the word has at least two characters. If it doesn’t, the messagedifferentis printed, and the characters are never read. (len()is explained in Exercise #30.)- The two conditions are joined with
and. The whole condition is true only when both parts are true. - There are no extra variables. It compares
word[1] == word[-2]directly. - It joins text with
+instead of using an f-string. Both give the same message.
Why the order of the and matters
Python reads an and from left to right, and it stops at the first part that is false. When len(word) > 1 is false, Python doesn’t look at word[1] == word[-2] at all. That is what protects the program from the error. For a, the length check fails, so word[1] is never evaluated.
If the two parts were written the other way round, word[1] == word[-2] and len(word) > 1, then word[1] would be read first, and a one-character string would stop the program with the IndexError again.
With the check, both of these inputs give different:
| Input | Length | Result |
|---|---|---|
a | 1 | The second and the second to last characters are different |
| (empty) | 0 | The second and the second to last characters are different |
For every other input tested, My Solution and the University Solution print identical output, including the two samples.
Things to notice
A three-letter word always matches. In a string with exactly 3 characters, the second character and the second to last character are the same character, the one in the middle. So abc gives ... are b, and xyz gives ... are y.
With two letters, they are the first and the last. For ab, the second character is b and the second to last is a, so they are different. For aa, both are a.
Capital letters count as different. For Aa, the second character is a and the second to last is A, so the program prints different. To ignore capital letters, see String Methods in Python.
A space is a character, too. For a b a, both characters are a space, so the message ends with a space, and nothing seems to follow the word are.
Key takeaways
- The second character is
word[1], and the second to last isword[-2]. - Asking for an index that doesn’t exist gives an
IndexError. - Check the length before using an index, when the string might be too short.
andstops at the first false part, so a safety check written first protects the part after it.- The order of the parts in an
andmatters. - An f-string and joining with
+can give the same message.
✦ 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.