Python Coding Exercise #56: End to Beginning
Part of the series Python Practice Exercise — part 56
Before you start: Know input() and len() · Know while loops with a condition · Know string indexes
The question
Please write a program which asks the user for a string. The program then prints out the input string in reversed order, from end to beginning. Each character should be on a separate line.
An example of expected behaviour:
Please type in a string: hiya
a
y
i
h
What is new here: indexes, and going backwards
Every character in a string has a position, called its index. Writing the string followed by an index in square brackets gives the character at that position:
word = "hiya"
print(word[0])
h
Counting from the beginning starts at 0. Python can also count from the end, using negative numbers. The last character is -1, the one before it is -2, and so on:
| Character | h | i | y | a |
|---|---|---|---|---|
| Index from the beginning | 0 | 1 | 2 | 3 |
| Index from the end | -4 | -3 | -2 | -1 |
This exercise wants the characters from the end to the beginning, which is exactly the order of the negative indexes: -1, -2, -3, -4. So the program can start an index at -1 and keep subtracting 1.
Before coding: when to stop
The loop needs three things, as in Exercise #51: a starting value, a condition and a change.
- Start:
index = -1, the last character. - Change:
index -= 1, to move one character towards the beginning. - Condition: keep going until the first character has been printed. The first character has the index
-len(string), which is -4 forhiya. So the loop continues whileindexis at least-len(string).
The condition matters, because an index that goes too far gives an error:
word = "hiya"
print(word[-5])
IndexError: string index out of range
My Solution
input_string = input("Please type in a string: ")
index = -1
while index >= -len(input_string):
print(input_string[index])
index -= 1
How My Solution Works
1. Read the string and start at the end
input_string stores what the user typed. index = -1 points at its last character.
2. The condition
while index >= -len(input_string): is checked before every round. For hiya, len(input_string) is 4, so the condition is index >= -4. It is true for -1, -2, -3 and -4, and false for -5. (See Exercise #30 for len().)
3. Print one character, then move back
print(input_string[index]) prints the character at the current index. print() puts each one on its own line. Then index -= 1 moves one position towards the beginning.
Following hiya
| Round | index at the check | index >= -4? | Python prints | index after |
|---|---|---|---|---|
| 1 | -1 | Yes | a | -2 |
| 2 | -2 | Yes | y | -3 |
| 3 | -3 | Yes | i | -4 |
| 4 | -4 | Yes | h | -5 |
| — | -5 | No | the loop stops |
The output is a, y, i, h, with each letter on its own line.
University Solution
input_string = input("Please type in a string: ")
index = -1
while index >= -len(input_string):
print(input_string[index])
index -= 1
The University Solution is exactly the same as My Solution.
Another way to write it
The same result can also be made with positive indexes, counting down from the last position to 0:
input_string = input("Please type in a string: ")
index = len(input_string) - 1
while index >= 0:
print(input_string[index])
index -= 1
Here, index starts at len(input_string) - 1, which is 3 for hiya, because the last character of a string with 4 characters has the index 3. The loop continues while index >= 0. Both versions print identical output for every input tested. The negative-index version is shorter to start, because -1 is always the last character, but the positive version has an easier condition, index >= 0.
Things to notice
An empty string prints nothing. The length is 0, so the condition is index >= 0 with index at -1. That is false immediately, and the loop never runs.
Spaces are printed as characters, too. For ab cd, the output is d, c, a line holding a single space, b and a. The line with the space looks empty, but it is there.
The condition prevents the IndexError. Without the check, or with a condition that allows -5 for a four-character string, Python would stop with string index out of range.
Each character is on its own line because every character is printed with its own print(). To print them all on one line, the loop would build a new string instead, as in Exercise #49.
Key takeaways
- Each character in a string has an index. From the beginning, indexes start at 0.
- Negative indexes count from the end: -1 is the last character.
string[index]gives the character at that index.- The first character has the negative index
-len(string), and this is where the loop stops. - A loop can walk through a string by changing an index each round.
- An index outside the string gives an
IndexError, so the loop condition must stop before that.
✦ 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.