191. Python Program to Reverse a String
EasyPhase 1 · Warm-up
This one needs only a for loop and a string that grows in it. You know both.
Overview
Reversing a string is one of the most common warm-up questions in coding interviews. It shows that you can walk through text one character at a time.
Problem Statement
Aiko writes each secret note backwards, so that it is harder to read.
Read one line of text and print it backwards, as Reversed: <text>. Every character moves, also the spaces.
Steps
- Read the line of text.
- Start with an empty string for the result.
- Loop over the characters. Put each one in front of the result.
- Print
Reversed:and then the result.
Examples
Example 1. Input hello gives:
Reversed: olleh
Why: the last letter o comes first, and the first letter h comes last.
Example 2. Input Good morning gives:
Reversed: gninrom dooG
Why: the whole line turns around. The space moves too, and the capital G ends up last.
Input
One line of text: a word or a sentence (1 character or more). Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.
Output
One line: Reversed: and then the text backwards. The tests try 5 different inputs.
Try it first. The solution opens when you solve this question, or after 3 tries with Submit.